Comma separated values in a column of table. This table was getting populated from some source via SSIS package.
There are two ways to split comma seprated list,
1. Using User defined function.
2. without user defined function
1. Using User defined function.
2. without user defined function
Here is the simplified version of the problem for Split Comma Separated List Without Using a Function In Sql server.
DECLARE @t TABLE
(
ID INT,
Tags VARCHAR(8000)
)
INSERT INTO @t VALUES
(1,'converssation,News,Events'),
(2,'Blogs,Stream,Documents'),
(3,'Org,Work,test')
SELECT * FROM @t
This is how data looks like in the table.
ID Tags
1 converssation,News,Events
2 Blogs,Stream,Documents
3 Org,Work,test
With the above data available, they want it split into individual values. Searching for possible solutions, here is the solution query I came up with:
With the above data available, they want it split into individual values. Searching for possible solutions, here is the solution query I came up with:
SELECT ID,
LTRIM(RTRIM(m.n.value('.[1]','varchar(8000)'))) AS Tags
FROM
(
SELECT id,CAST('<XMLRoot><RowData>' + REPLACE(Tags,',','</RowData><RowData>') + '</RowData></XMLRoot>' AS XML) AS x
FROM @t
)t
CROSS APPLY x.nodes('/XMLRoot/RowData')m(n)
Now the Output looks like below.
ID Tags
1 converssation
1 News
1 Events
2 Blogs
2 Stream
2 Documents
3 Org
3 Work
3 test
