OrangeScape Developer Community

A community for Application Developers on OrangeScape's Platform-as-a-Service

Concatenate string column values for a given group into a comma (or any character) separated list of values in SQL Server 2005+

Sometimes we need to convert (concatenate) string column values for a given group into a comma (or any character) separated list of values.
e.g.:
We have a table as below:


We want an output as below:


here is one way of doing this in Ms-SQL 2005/2008 using FOR XML PATH(”)
Select NoteID, STUFF ((SELECT ',' + a.Comments
from Notes a where a.NoteID = b.NoteID Order by a.Comments
for xml PATH('')),1,1,'') AS Comments_Concatenated
from Notes b group by NoteID ORDER BY NoteID

The STUFF function removes the leading comma

Views: 56

Comment

You need to be a member of OrangeScape Developer Community to add comments!

Join OrangeScape Developer Community

Comment by Gowdhaman Rajakrishnan on December 31, 2010 at 7:52pm

Good Posts . But MySQL is quite simple

SELECT GROUP_CONCAT(c.companyname) FROM company c GROUP BY c.companytype

 :)

 

 

 

© 2017   Created by OrangeScape Technologies.   Powered by

Badges  |  Report an Issue  |  Terms of Service