There are few types where you can get comma separated values form SQL SERVER using SQL
1. XML PATH method:
SELECT STUFF((SELECT ', ' + column_name FROM table_name WHERE conditions FOR XML PATH('')), 1, 2, '') AS csv_values;
2. COALESCE and FOR XML method:
SELECT STUFF(( SELECT ', ' + column_name FROM table_name WHERE conditions FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 2, '') AS csv_values;
3. FOR XML PATH method
SELECT STUFF((SELECT ',' + column_name FROM table_name WHERE conditions FOR XML PATH('')), 1, 1, '') AS csv_values;
In these examples, replace column_name
with the actual column name and table_name
with the appropriate table name. Customize the WHERE
clause to filter the desired rows if necessary.
When executing these queries, a single row with a single column will be returned, containing the comma-separated values from the specified column. Please note that the XML-related methods convert the values to XML and then manipulate them, resulting in a string of comma-separated values.
Hope this helps!!