Exporting data from SQL Server Express to CSV (need quoting and escaping)

It can be done! However you have to specifically configure SSMS to use quoted output, because for some daft reason it is not the default.

In the query window you want to save go to Query -> Query Options…

Check the box “quote strings containing list separators when saving .csv results”.

enabling quoted csv output

then

select 'apple,banana,cookie' as col1,1324 as col2,'one two three' as col3,'a,b,"c",d' as col4

will output

col1,col2,col3,col4
"apple,banana,cookie",1324,one two three,"a,b,""c"",d"

which is what we all want.

Update 2022-Aug-08

I just installed SSMS version 18.12.1. Inexplicably, Microsoft has removed the “quote strings” option from both the Query -> Query Options -> Results -> Grid method described above and the Tools -> Options -> Query Results -> SQL Server -> Results to Grid method suggested by Tim Partridge. I have no solution for this version of SSMS.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)