Possible Duplicate:
Simulating group_concat MySQL function in MS SQL Server 2005?
SQL Query for aggregation/concatenation
I am trying to use the stuff function in SQL server to stuff certain info. Here is the example:
Money Age Gender
860 9 F
860 15 M
860 15 M
860 16 M
860 16 F
I would like to stuff the Age and Gender column so that only one record will display as following:
Money Age Gender
860 9, 15, 15, 16, 16 F, M, M, M, F
Please note, I would like to keep the two 15s and three M in the Age and Gender respectively.
This is easier to do using a FOR XML rather than stuff. Keep in mind that the FORM XML clause can be tricked to generate a comma seperated list (CSV).
The example below should do exactly what you are asking for.
and here is the output.
I hope this helps.
If you need more details, I have a blog posting from last year that explains this.
http://stevestedman.com/2011/10/converting-part-of-a-result-set-to-a-comma-separated-list/