0

我需要一个查询来分别在大括号之间拆分值。

我的 varchar 值是

16(8),14(10)

我需要拆分为

16,14

我只需要1614值,但不需要(8) (10)大括号内的值

我试过这个查询

select
  case 
    when charindex('(0-9)', OtherProduct) > 0
    then rtrim(left(OtherProduct, charindex('(0-9)', OtherProduct)-1))
    else OtherProduct end as OtherProduct
from dbo.rxnreactions where rsd='P=15(61),16(8),14(10)R=1,7S=9012'

有谁能帮我把它分开。

4

1 回答 1

0

使用函数用逗号分割,然后用括号分割,最后加入一个字符串

SELECT SplitByBrackets.val 
FROM dbo.StringSplit(N'16(8),14(10)',N',') SplitByComma
CROSS APPLY StringSplit(SplitByComma.val,N'(') SplitByBrackets
WHERE SplitByBrackets.id % 2 = 1

StringSplit 的样本是

CREATE FUNCTION [dbo].[StringSplit]
(
  @delimited nvarchar(max),
  @delimiter nvarchar(100)
) RETURNS @t TABLE
(
-- Id column can be commented out, not required for sql splitting string
  id int identity(1,1), -- I use this column for numbering splitted parts
  val nvarchar(max)
)
AS
BEGIN
  declare @xml xml
  set @xml = N'<root><r>' + replace(@delimited,@delimiter,'</r><r>') + '</r></root>'

  insert into @t(val)
  select 
    r.value('.','varchar(max)') as item
  from @xml.nodes('//root/r') as records(r)

  RETURN
END

摆弄它

于 2013-07-18T05:17:06.600 回答