How to export SQL Server 2005 query to CSV
Solution 1
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.
Solution 2
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.
Solution 3
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.
Solution 4
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.
Solution 5
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.
Konstantin Gizdarski
Full-stack software developer, solution architect, and Agile practitioner. Founder and principal consultant at ClearEye Consulting.
Updated on June 20, 2020Comments
-
Konstantin Gizdarski almost 4 years
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?
-
Konstantin Gizdarski about 15 yearsThis answers my question about the bcp (it is as ugly as I thought; nobody said bcp wasn't ugly). Thanks.
-
DForck42 about 15 yearsThis is what i would have posted
-
Konstantin Gizdarski over 14 yearsThere 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?
-
Konstantin Gizdarski over 14 yearsOne other note: -q leaves the sqlcmd editor open. -Q closes it.
-
Konstantin Gizdarski over 14 yearsI assume this is in combination with curtisboy's suggestion, right? Good idea.
-
JohnW over 12 yearsprepend "SET NOCOUNT ON;" to the query in order to suppress the number of rows from being displayed.
-
RegisteredUser over 12 yearsThis doesn't work well for large result sets... memory exceptions. :(
-
Joe Phillips over 12 yearsWhen you save results to CSV it doesn't seem to escape commas in varchar fields properly
-
Josh Kodroff about 12 yearsWhy oh why would that not be the default behavior? That's beyond dumb that you have to set that. Thanks for the tip, though. Very useful.
-
Kevin Day over 11 yearsFor what it's worth, the "quote strings" option didn't work for me (emitted strings weren't quoted). I wound up having to include the quotation marks in my SELECT statement. Leave it to Microsoft to make a data export tool that is completely useless.
-
sparc_spread about 10 yearsI know this was a long time ago, and I am on SQL Server 2008, but one thing I wanted to add is this: make sure that after you change the quote setting, you open a new query window to run the query and export the results from. It doesn't work unless you do that. Just adding this for the benefit of anyone else who reads this answer.
-
Jan Šotola about 10 yearsWhen exporting to CSV it also does not escape EOLNs within text fields (there is a workaround to wrap the exported data column with double quotes rightn when selecting from the database).
-
avs099 about 9 yearssee this one: stackoverflow.com/a/2426853/1246870: -W (to remove trailing spaces) was a life-saver for me
-
Jon Mabe almost 9 yearsYou'll also want to change Query Options in Management Studio to fix field escaping and add column headers. -- Go to Query -> Query Options -> Results and then select to include column headers and quote strings.
-
David over 8 yearsSSIS is actually a poor way to do most things. Its useful if you need to write code but don't know SQL or another programming language, otherwise it is slow, bloated, doesn't use transactions in a sane way, and difficult to debug. Theres no point in an entire SSIS package when a simple BCP call will do.
-
Konstantin Gizdarski almost 7 yearsHeh. There's thinking out of the box. I was working in SQL Server at the time, and couldn't figure out a good way to do it from there. I never thought to start from the other side. Good suggestion!