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

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


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s