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

前端 未结 12 1009
梦谈多话
梦谈多话 2020-12-04 07:57

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,

12条回答
  •  盖世英雄少女心
    2020-12-04 08:40

    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.

提交回复
热议问题