How can I export data to csv format in SQL Server 2005?

Filed under: Administration, SQL, SQL Server 2005 — Ari Weil at 12:13 pm on Friday, June 27, 2008

The easiest way to do this would be to use the SQLCMD command-line utility to export a query or query file result set, changing the column separator value to a comma.

SQLCMD -S MyInstance -E -d sales -i query_file.sql -o output_file.csv -s ,

Look at the hyperlink listed above for more SQLCMD options that can make automating many of your everyday tasks simple.

3 Comments »

Trackback by Sascha's blog

July 11, 2008 @ 7:47 am

CSV Export…

Eine Möglichkeit ‘CSV’ zu exportieren. Achtung: eine Zeile voller ‘-’.

Beitrag als PDF an ……

Comment by Michael B

July 11, 2008 @ 12:18 pm

Would that actually work, though? If it’s not text-delimited, then something like a full_name field with a value of Michael B, esq. would choke it. So you would do “Michael B, esq.”, but I don’t see anything telling it to do that for text fields (and so leave the numeric fields as strictly numeric).

But then again, I typically use pipe-delimited, since having to deal with the commas is a pain.

Comment by Ari Weil

July 11, 2008 @ 12:50 pm

That’s the idea… If you really wanted to export to csv you could DTS the data out, but if you’re looking for a quick way to get the data, then changing the delimiter to something you can work with would be the necessary workaround. So, if you are working with the TEXT datatype, you’d have to pick a pipe or other equivalent separator. You could use a pipe by changing to -s “|”.

RSS feed for comments on this post. TrackBack URI

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>