Sunday, March 8, 2015

SQL server Multiple rows to one comma separated value

This is the select statement in Sql server which will select the multiple rows of a column into a single column and single row as a comma separated values.

SELECT  ID ,STUFF((SELECT ', ' + CAST(Name AS VARCHAR(10)) [text()] FROM Customer WHERE GroupId = t.ID FOR XML PATH(''), TYPE).value('.','NVARCHAR(MAX)'),1,2,' ') List_Output
FROM CustomerGroup t
GROUP BY ID

You can test the functionality here http://sqlfiddle.com/#!6/1cda0e/4

No comments:

Post a Comment