Export sql results to csv

  •  

    Hi all,

    Hopefully you can help me.

    I have created a sql job which returns some results and then exports it to a csv file. But each column is merged into one cell.

    The way I have done this is using the sqlcmd feature as per the below:

    sqlcmd -i <file path>

    Is there any way of exporting my query results to a csv file where each column is in a seperate cell?

    Thanks,

    Dan

     

  • I am thinking you are using the -o parameter and not the -i one (or possibly both), right?  -i is for an input file, -o is for output file.  Well, unless you have an input file, then you'd use -i <file path>.

    So, assuming you have an input file and want to export to CSV, you would use a command like this:

    sqlcmd -i <input file path> -o <output file path> -s ,

    If you want that to run against a specific server, you'd add -S <servername>.

    The -s (lowercase S) allows you to specify a column separator.  A CSV file opened in Excel expects it to be a comma.

    The above is all just my opinion on what you should do. 
    As with all advice you find on a random internet forum - you shouldn't blindly follow it.  Always test on a test server to see if there is negative side effects before making changes to live!
    I recommend you NEVER run "random code" you found online on any system you care about UNLESS you understand and can verify the code OR you don't care if the code trashes your system.

Viewing 2 posts - 1 through 1 (of 1 total)

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