I've a query which I need to convert to CSV, but to return the CSV as a query result, not save to/as a file.
All I can find are examples on how to "save" a queries' output as a CSV (Export table from database to csv file), but I need to have the CSV as an output of a query so it can be returned like a normal query.
Is there a more elegant/efficient method other than doing...
SELECT STUFF((
SELECT ',' + CSV
FROM(
SELECT
CONVERT(VARCHAR,Stat_Date,103) + ','
+ COALESCE(UPRN, '') AS CSV
FROM [stats].[tvfSomeFunction](@TargetDate)
) x
FOR
XML PATH('')), 1, 1, '') CSV
As theres in addition to lots of rows, also lots of columns, so this method is tedious to create/maintain.