October 02, 2012

MS SQL Comma Separated Values

SELECT 
 c1.ID,
 STUFF((SELECT ',' + CountyName  FROM dbo.County AS c2
 WHERE c2.ID = c1.ID
 FOR XML PATH('')),1,1,'') AS CSV
FROM dbo.County AS c1
GROUP BY c1.ID
GO