I want to split a string in this format Quote:
\"date=10/10/2000|age=13^date=01/01/2001|age=12^date=02/02/2005|age=8\"
. Actually this stri
try this:
Declare @stringToSplit varchar(max)='date=10/10/2000|age=13^date=01/01/2001|age=12^date=02/02/2005|age=8^date=10/10/2000|age=13^date=01/01/2001|age=12^date=02/02/2005|age=8^date=10/10/2000|age=13^date=01/01/2001|age=12^date=02/02/2005|age=8^date=10/10/2000|age=13^date=01/01/2001|age=12^date=02/02/2005|age=8'
DECLARE @YourTable table (RowID int, Layout varchar(max))
INSERT @YourTable VALUES (1,@stringToSplit)
;WITH SplitSting AS
(
SELECT
RowID,LEFT(Layout,CHARINDEX('^',Layout)-1) AS Part
,RIGHT(Layout,LEN(Layout)-CHARINDEX('^',Layout)) AS Remainder
FROM @YourTable
WHERE Layout IS NOT NULL AND CHARINDEX('^',Layout)>0
UNION ALL
SELECT
RowID,LEFT(Remainder,CHARINDEX('^',Remainder)-1)
,RIGHT(Remainder,LEN(Remainder)-CHARINDEX('^',Remainder))
FROM SplitSting
WHERE Remainder IS NOT NULL AND CHARINDEX('^',Remainder)>0
UNION ALL
SELECT
RowID,Remainder,null
FROM SplitSting
WHERE Remainder IS NOT NULL AND CHARINDEX('^',Remainder)=0
)
select SUBSTRING(part,CHARINDEX('=',part)+1,(CHARINDEX('|',part)-CHARINDEX('=',part))-1) as [Date],RIGHT(part,CHARINDEX('=',reverse(part))-1) as [Age] from SplitSting