views:

7228

answers:

9

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?

+8  A: 

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.

Jose Basilio
I knew it was something simple like that. Thanks.
John M Gant
+2  A: 

For adhoc queries:

Show results in grid mode (CTRL+D), run query, click top left hand box in results grid, paste to Excel, save as CSV. You may be able to paste directly into a text file (can't try it now)

Or "Results to file" has options too for CSV

Or "Results to text" with comma separators

All settings under Tool..Options and Query.. options (I think, can't check) too

gbn
This is what i would have posted
DForck42
+2  A: 
Sijin
Didn't know you could do that. Thanks.
John M Gant
+1  A: 

Yeah, there is a very simple utility in Management Studio, if you're just looking to save query results to a CSV.

Right click on the result set, the select "Save Results As". The default file type is CSV.

Gus
+3  A: 

If it fits your requirements, you can use bcp on the command line if you do this frequently or want to build it into a production process.

Here's a link describing the configuration.

le dorfier
This answers my question about the bcp (it is as ugly as I thought; nobody said bcp wasn't ugly). Thanks.
John M Gant
+1  A: 

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.

curtisboy
There are a couple of problems with this approach. It doesn't include the quotes, and it prints a "number of rows affected" message at the bottom of the file. Do you know a way to get around either of those?
John M Gant
One other note: -q leaves the sqlcmd editor open. -Q closes it.
John M Gant
+1  A: 

set nocount on

the quotes are there, use -w2000 to keep each row on one line.

stevej6x7
I assume this is in combination with curtisboy's suggestion, right? Good idea.
John M Gant
A: 

In SQL 2005, this is simple: 1. Open SQL Server management studio and copy the sql statement you need into the TSQL , such as exec sp_whatever 2. Query->Results to Grid 3. Highlight the sql statement and run it 4. Highlight the data results (left-click on upper left area of results grid) 5. Now right-click and select Save Results As 6. Select CSV in the Save as type, enter a file name, select a location and click Save.

Easy!

Scott
A: 

I had to do one more thing than what Sijin said to get it to add quotes properly in SQL Server Management Studio 2005. Go to

Tools->Options->Query Results->Sql Server->Results To Grid

Put a check next to this option:

Quote strings containing list separators when saving .csv results

Note: the above method will not work for SSMS 2005 Express! As far as I know there's no way to quote the fields when exporting results to .csv using SSMS 2005 Express.

goku_da_master