How to export SQL Server 2005 query to CSV
I want to export some SQL Server 2005 data to CSV format (comma-separated with quotes). I can think of a lot of complicated ways to do it, but I want to do it the right way. I've looked at bcp, but I can't figure out how to put the quotes around the fields (except concatenating them to the field values, which is ugly). I guess I could do it with sqlcmd and -o, but that seems ugly for the same reason.
Is there a bcp way to do it?
Is there a reasonable sqlcmd way to do it?
Is there some great, simple utility built into the Management Studio that I'm just overlooking?
In Management Studio, select the database, right-click and select Tasks->Export Data
. There you will see options to export to different kinds of formats including CSV, Excel, etc.
You can also run your query from the Query window and save the results to CSV.
In management studio, set query options to output to file, and in options->query results set output to file to output using comma as delimiter.
If you can not use Management studio i use sqlcmd.
sqlcmd -q "select col1,col2,col3 from table" -oc:\myfile.csv -h-1 -s","
That is the fast way to do it from command line.