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,
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.