Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to export data as CSV format from SQL Server using sqlcmd?

I can quite easily dump data into a text file such as:

sqlcmd -S myServer -d myDB -E -Q "select col1, col2, col3 from SomeTable" 
     -o "MyData.txt"

However, I have looked at the help files for SQLCMD but have not seen an option specifically for CSV.

Is there a way to dump data from a table into a CSV text file using SQLCMD?

like image 759
Ray Avatar asked Sep 29 '22 06:09

Ray


1 Answers

You can run something like this:

sqlcmd -S MyServer -d myDB -E -Q "select col1, col2, col3 from SomeTable" 
       -o "MyData.csv" -h-1 -s"," -w 700
  • -h-1 removes column name headers from the result
  • -s"," sets the column seperator to ,
  • -w 700 sets the row width to 700 chars (this will need to be as wide as the longest row or it will wrap to the next line)
like image 156
scottm Avatar answered Oct 20 '22 23:10

scottm