Yes, FOR XML has been widely accepted for years as the fastest T-SQL way to do this. Be careful though if your data contains reserved XML characters (,&,etc) because they will be “escaped” in your result.

As a side note, your heading says “USING STUFF”, but the concatenation is actually done by FOR XML PATH(”). STUFF is just used to delete the leading 2 characters (comma space).

[…] SQL SERVER – Create Comma Separated List From Table – I was preparing a statistical report and was stuck in one place where I needed to convert certain rows to comma separated values and put into a single row. […]

Blog Stats

Follow Blog via Email

Disclaimer

This blog/website is a personal blog/website and all articles, postings and opinions contained herein are my own. The contents of this blog/website are not intended to defame, purge or humiliate anyone should they decide to act upon or reuse any information provided by me. Comments left by any independent reader are the sole responsibility of that person. Should you identify any content that is harmful, malicious, sensitive or unnecessary, please contact me via email (imran@raresql.com) so I may rectify the problem.