I have a table called SampleData which looks like this:
col1 col2
1 a
1 b
1 c
2 d
2 e
3 f
I need the data in the below format:
col1 col2
1 a,b,c
2 d,e
3 f
Is there a way of doing this using CTE as well?
you can use STUFF if you are using SQL Server 2005 and above.
SELECT
[col1],
STUFF(
(SELECT ',' + [col2]
FROM Table1
WHERE [col1] = a.[col1]
FOR XML PATH ('')) , 1, 1, '') AS col2
FROM Table1 AS a
GROUP BY [col1]
I think this is also useful to you.
Comma Seprate Value
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With