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<
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.
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.
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.
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 Sql Server 2012 - Management Studio:
Solution 1:
Execute the query
Right click the Results Window
Select Save Results As from the menu
Select CSV
Solution 2:
Right click on database
Select Tasks, Export Data
Select Source DB
Select Destination: Flat File Destination
Pick a file name
Select Format - Delimited
Choose a table or write a query
Pick a Column delimiter
Note: You can pick a Text qualifier that will delimit your text fields, such as quotes.
If you have a field with commas, don't use you use comma as a delimiter, because it does not escape commas. You can pick a column delimiter such as Vertical Bar: | instead of comma, or a tab character. Otherwise, write a query that escapes your commas or delimits your varchar field.
The escape character or text qualifier you need to use depends on your requirements.
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