Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 398
John M Gant Avatar asked Apr 28 '09 19:04

John M Gant


People also ask

How do I export a SQL 2005 database?

To export the data, you can: Right mouse click on your database >> tasks >> export data. or copy/paste from/to table data view.

How do I export SQL query result to text file in SQL Server?

However, if you prefer to export SQL query results to a text file via a Wizard, we have your back. To begin with, right-click the database in SQL Server Management Studio or SSMS. Then, select the Import or Export data option and head to Export Data under Tasks. Next, open the SQL Server Import and Export wizard.


2 Answers

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.

like image 73
Jose Basilio Avatar answered Nov 12 '22 00:11

Jose Basilio


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.

like image 37
Sijin Avatar answered Nov 12 '22 01:11

Sijin