隔离括号内的字符

时间:2011-07-15 17:18:07

标签: sql

考虑一个包含以下数据的文本字段:

Plastic Bottle (5 ml)

如何隔离括号内的文本以便我可以在另一个语句的上下文中使用它?

请注意,括号内的文字会有所不同

1 个答案:

答案 0 :(得分:3)

这样的东西应该有效(但它只会在第一组括号中提取文字)......

DECLARE @Input AS VARCHAR(MAX)
DECLARE @Extracted AS VARCHAR(MAX)
DECLARE @Open AS INT
DECLARE @Close AS INT

SET @Input = 'Plastic Bottle (5 ml)'
SET @Open = PATINDEX( '%(%', @Input ) + 1
SET @Close = PATINDEX( '%)%', @Input ) - 1
SET @Extracted = SubString( @Input, @Open, @Close - @Open + 1 )

PRINT @Extracted