T-SQL Export to CSV help

  • are you replacing <SERVERNAME> with your server name right?

    Can you please check the syntax try to print the @cmd and check.

  • Hi, Sorry yes I am replacing <SERVERNAME>. Thanks

    So the cmd looks like -

    sqlcmd -S MyServerName -E -h -1 -d AdventureWorks -Q "SET NOCOUNT ON; SELECT U.[UserId] ,U.[UserName] ,U.[DateCreated] FROM dbo.Users U" -o "C:\test_output_file.csv" -s","

  • Remove "SET NOCOUNT ON" and try to execute you command

  • Hi,

    Thanks for your reply. The reason why I added that was to remove the row count on the output of the CSV. However I removed the section as you suggested and the output was with whitespace. See below -

    CB8ECBE0-EDEB-41CB-B8B1-B15DC8F1A70E,Temp123 ,2014-01-21 10:03:32.750

    66D53C8E-E543-4B55-983C-F0BE97936655,User123 ,2014-01-22 10:03:32.753

    (2 rows affected)

  • There's a -W parameter to SQLCMD that removes trailing spaces from fields--would that work for you?

  • paul.knibbs (1/22/2014)


    There's a -W parameter to SQLCMD that removes trailing spaces from fields--would that work for you?

    Thanks Paul, Passing the -W parameter did the trick.

    Many Thanks

  • Instead of using SQLCMD for this, I'd recommend the use of BCP instead (with the understanding that it won't give you column headers).

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 7 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic. Login to reply