How to get export output in "real" CSV format in SQL Server Management Studio?

I have a query that I am running in SQL Server Management Studio (connecting to a SQL Server 2005 database). I want to export the data in CSV format. Not wannabe CSV format, where you just stick a comma between each column, but "real" CSV format, where you put quotes around your strings. This way you can export data that has commas or quotes in it.

All the examples I see limit themselves to the wannabe format. I can't figure out where the option to quote strings is.

If SSMS is truly incapable of this basic feat, are there other tools that will do it easily? I don't want to have to write a C# program every time I need a data dump.


In SSMS 2012 there's an option for this, in Tools -> Options -> Query Results -> SQL Server -> Results to Grid, it's called "Quote strings containing list separators when saving .csv results". I don't know how long such an option has existed for, but I'm baffled by two things:

  1. How come it's not turned on by default
  2. How come it's an option and not an intrinsic part of the CSV exporting code

It just defies belief that the default behaviour is to have CSV export that's impossible to import properly. I've noticed Excel does the same, I'll have to go see if that's got an option too.

In the mean time, thanks to my colleague who pointed me to this bizarre bit of functionality when I was ranting about how the CSV exporter was completely useless, and this was the best link I'd found about it so I thought I'd put the knowledge here for the benefit of future searchers.

UPDATE

A screenshot below:enter image description here


My normal work-around is to build it into the query:

SELECT '"' + REPLACE(CAST(column AS NVARCHAR(4000)), '"', '""') + '"' AS Header, ... FROM ...

You can build that into a user-defined function, to make it a little easier, but you have to build a separate function for each data type.


Different combinations of these settings can bring results in the output that are incorrect or partial data. This is because Microsoft didn't think it was important enough to fix these issues. I'm only explaining what happens with CSV files when sending the results to a file.

To get good results, do the following:

Open new query window (new tab/session) ... if you do not, configuration below is lost and set back to the defaults

Write the query to handle the quote inside the quote, and also wrap all string data types in quotes. Also be aware that different DBMS and programming language grammars accept a different syntax for an escaped double quote (if using this output as input to another system). Some use \". Some use "". XML uses ". Probably a reason Microsoft chose to ignore this functionality, so they didn't have to deal with the arguments.

.. If Escape Sequence of new system is "".

SELECT '"' + REPLACE(CAST(column1 AS VARCHAR(MAX)), '"', '""') + '"' FROM table1

.. If Escape Sequence of new system is \".

SELECT '"' + REPLACE(CAST(column1 AS VARCHAR(MAX)), '"', '\"') + '"' FROM table1

Configuration:

Query Options > Results > "Include column headers when copying or saving the results" checked

Query Options > Results > "Quote strings containing list separators when saving .csv results" - BROKEN; DO NOT USE!

Query Options > Results > others unchecked

Query Options > Results > Text > comma delimited (setting on top right corner)

Query Options > Results > Text > "Include column headers in the result set" checked

Query Options > Results > Text > others unchecked

Query Options > Results > Text > "Maximum number of characters displayed in each column" - set to max length so strings don't get truncated.

Query > Results To File (this is a toggle between all 3 options)

Execute query (F5)

Prompt for file name of report

Open file to look at results

NOTE: If you need to do this on a regular basis, you're better off just developing a program that will do this for you in .NET or Java, or whatever language you are comfortable with. Otherwise you have a high probability of making a mistake. Then be extremely aware of the syntax of the system you're importing into, before you define your export out of SQL Server.


It's sad the option is available in a confusing state, yet not perfectly operational. The following is working at least.

  1. Choose "Tasks>Export Data" from the DB context menu (does not work at Table level either)
  2. For Source, choose "Microsoft OLE DB Provider for SQL Server"
  3. For destination choose "Flat File...", and specify "Format" as delimited and text qualifier as double-quote
  4. Select Table or query (I worked with query)
  5. Finish the wizard

you should be good to go!


How do you feel about Export to CSV from SSMS via PowerShell? This post describes how to define an external tool in SSMS that sends the currently selected query to a PowerShell script which exports to a CSV.