SQL: Exporting SQL Data in CSV format

Reference: http://ask.sqlservercentral.com/questions/7080/export-results-of-a-query-into-a-csvtxt-file.html

Method 1:

Straight to file

In Management Studio, open a query window and set the ‘results to file’ by either

· Query -> results to -> Results to file

· ctrl – Shift – F

· click the ‘Results to file’ icon on the toolbar

set the output to be comma separated

· (Tools -> Options -> Query results -> SQL Server -> Results to text -> Output format = ‘Comma delimited’

then run your query

select * from public_view where datefield = currentdate

Method 2:

In Management Studio, open a query window and set the results to text by either

· (Query -> results to -> Results to text

· ctrl – T

· click the ‘Results to text’ icon on the toolbar

set the output to be comma separated

· (Tools -> Options -> Query results -> SQL Server -> Results to text -> Output format = ‘Comma delimited’

then run your query

select * from public_view where datefield = currentdate

select all the results and paste into a file

Method 3:

Use BCP

probably best to review this in BOL

xp_cmdshell 'bcp "query" out filename.csv'
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s