Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

in SqlServer management studio, you can export query results to a csv file by right click -> "Save Results as" -> csv. However, the csv file does not contain column headers and does not escape any commas in the data itself, which often leads to a malformed file.

How can I export query results with headers and commas properly handled?

share|improve this question
up vote 4 down vote accepted

See Options > Query Results > SQL Server > Results to Text

Check "include column headers". My text results are being enclosed in doublequotes so that should take care of the comma issue. Take a look at the "Results to grid" tab as well - I see a "Quote strings containing list separators when saving .csv results"

If not through SSMS then you can also use the sqlcmd command line utility which includes more options than the powershell equivalent.

share|improve this answer
+1 That did it. In the "results to grid" options I needed "include column headers..." and "quote strings...". – Gabe Moothart Sep 17 '10 at 14:52

Not through Management Studio, but you can do this with the powershell integration in 2008. right-click in the object explorer and click "Start Powershell", then:

Invoke-Sqlcmd -ServerInstance '.\sqlexpress' -Database 'temp' -Query "select * from t" | Export-Csv file.csv -notype
share|improve this answer
This method works a little better than the accepted answer, though it requires use of PowerShell. Using this from now on to export CSV. – Giscard Biamby Jan 5 '13 at 20:57

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.