sql server - SQL Split String on Delimiter and set to new columns -
there has been questions related asked, have not gotten solution need of.
the string(s) trying split this:
/dev/act/billing or
/st/recmanage/prod/form the issue have first '/' giving me problems when try , things left/right/substring/charindex. messes counts , stopping @ delimiter. also, important note number of delimiters changes. want find way split can every possible substring.
right(c3.path,charindex('/', reverse(c3.path))-1) lastpath this has gotten me last part of string. have messed other things ilke:
substring(c3.path,charindex('/',c3.path,(charindex('/',c3.path)+1))+1,len(c3.path)), this gets after second '/'
i have messed xml and
set @delimiter='/' ;with cte (select cast('<m>' + replace([path], @delimiter , '</m><m>') + '</m>' xml) [type xml] [rpts].[dbo].[cata] ) , cte2 (select [type xml].value('/m[2]', 'varchar(50)') firstpath cte) then doing: cte2.firstpath result. gives null
i not on sql 2016 cannot use split_string.
thank
try this:
declare @mockup table(id int identity,yourstring varchar(max)); insert @mockup values('/dev/act/billing'),('/st/recmanage/prod/form'); select m.id ,b.part.value(n'text()[1]',n'nvarchar(max)') @mockup m outer apply(select cast('<x>' + replace(m.yourstring,'/','</x><x>') + '</x>' xml)) a(casted) outer apply a.casted.nodes(n'/x[text()]') b(part); this approach save long not have forbidden characters in string (namely <, > , &). if need this, call in, possible solve.
using .nodes() xquery predicat [text()] ommit rows no value...
the result
id part --------- 1 dev 1 act 1 billing 2 st 2 recmanage 2 prod 2 form
Comments
Post a Comment