How to split values from braces using SQL query -
i need query split values between braces separately.
my varchar value is
16(8),14(10)
i need split as
16,14
i need 16 , 14 value not values inside (8) (10)
braces
i have tried query
select case when charindex('(0-9)', otherproduct) > 0 rtrim(left(otherproduct, charindex('(0-9)', otherproduct)-1)) else otherproduct end otherproduct dbo.rxnreactions rsd='p=15(61),16(8),14(10)r=1,7s=9012'
is can me split it.
use function split comma, split brackets, , @ end join single string
select splitbybrackets.val dbo.stringsplit(n'16(8),14(10)',n',') splitbycomma cross apply stringsplit(splitbycomma.val,n'(') splitbybrackets splitbybrackets.id % 2 = 1
sample of stringsplit is
create function [dbo].[stringsplit] ( @delimited nvarchar(max), @delimiter nvarchar(100) ) returns @t table ( -- id column can commented out, not required sql splitting string id int identity(1,1), -- use column numbering splitted parts val nvarchar(max) ) begin declare @xml xml set @xml = n'<root><r>' + replace(@delimited,@delimiter,'</r><r>') + '</r></root>' insert @t(val) select r.value('.','varchar(max)') item @xml.nodes('//root/r') records(r) return end
Comments
Post a Comment