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,
I think the easiest is to open excel and import the data from SQL connection rather than using SSMS export.... I'm using SSMS 2016 and it doesn't have the option "Quote strings containing list separators when saving .csv results" presumably because it doesn't work
Ron
It's sad the option is available in a confusing state, yet not perfectly operational. The following is working at least.
text qualifier
as double-quoteyou should be good to go!
As all the settings mentioned above didn't fix the CSV my SSMS (SQL Server 2014) generated and exporting a tab-separated file didn't make it any better, a colleague and me made a converter script (Ruby) to convert the SSMS CSV into readable CSV. It keeps encoding, separators and linebreaks of the original file and even does an exact-byte-match validation at the end (it creates a file in the SSMS format from the parsed (!) input file and compares both files).
https://gist.github.com/gr8bit/62202ea89a7e3aff67df2ff080ee8e88
Contact me on github if you encounter errors, please. Cheers!
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:
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:
Usually I use this kind of function:
CREATE FUNCTION [dbo].[toExport]
(
@txt varchar(max)
)
RETURNS varchar(max)
AS
BEGIN
return REPLACE(REPLACE(REPLACE(@txt, ';', ','), CHAR(10), ' '), CHAR(13), ' ');
END
And in select I put it here:
SELECT dbo.toExport( column_name ) AS column_name FROM ....
And in SMSS 2012 simply Right click on a grid and save results as, or copy all grid (ctrl-A) and ctrl-V to Excel.
It's easiest way to manage data in for example MS Excel without problems with columns.
Of course you must click "Quote strings containing list separators when saving .csv results" in Tools -> Options -> Query Results -> Sql Server -> Results to Grid and increase Maximum Characters Retrieved if you need it.
I know of no way to do this with SSMS alone. I know TOAD (http://www.toadworld.com/) has a CSV option. Not sure if it is an escaped format. If SSIS is an option, you can convert to a format that escapes strings (true CSV), but that is not in SSMS.
If you have to write a C# program, I would consider querying the table and then running the query, as the metadata will clue which need the escape.