I need in SQL Server to split the following string:
'1:1:0:0:1231,1:0:0:0101:1231'
First I need to split into 2 rows:
- 1:1:0:0:1231
- 1:0:0:0101:1231
And finally I have to split each row in separate columns with the following name:
isactive--|--year--|--anniversay--|--startperiod--|--endperiod
1 1 0 0 1231
1 0 0 0101 1231
I have the following function for split but it just work for the first split (,) because it doesnt work for the columns.
CREATE FUNCTION [dbo].[Split]
(
@String NVARCHAR(4000),
@Delimiter NCHAR(1)
)
RETURNS TABLE
AS
RETURN
(
WITH Split(stpos,endpos)
AS(
SELECT 0 AS stpos, CHARINDEX(@Delimiter,@String) AS endpos
UNION ALL
SELECT endpos+1, CHARINDEX(@Delimiter,@String,endpos+1)
FROM Split
WHERE endpos > 0
)
SELECT 'Id' = ROW_NUMBER() OVER (ORDER BY (SELECT 1)),
'Data' = SUBSTRING(@String,stpos,COALESCE(NULLIF(endpos,0),LEN(@String)+1)-stpos)
FROM Split
)
GO
Any clue?