Menu & Search
Exporting SQL Server Query Results to CSV

Exporting SQL Server Query Results to CSV


This post is a quick guide on how to export data from SQL Server into a CSV file. The default export to settings will try save your query as a column aligned .rpt file, so I’ll cover that below while converting to CSV format.


Export SQL Server Query to CSV File

To export a query the simplest way, it would be for us to click Results to File as marked below.

SSMS Results to File Option

When you run the query, it’ll prompt asking where and what to save the file as.

.rpt File SQL Server

The default file type is as shown above, a .rpt file. If we open that in NotePad++, it’s looking like this;

Open .rpt File Notepad++

We’ll save it as CSV this time.

Save .rpt file as .csv

You’ll probably want to change the delimiter after looking at the file.

.rpt file in Excel

Set Output File to Column Aligned

We’ll have to go into the options of SSMS as shown below.

SSMS Tools Options

Into Results to Text, we can see the output format is set to Column aligned.

SSMS Results to Text Option

We’ll change that default value to Comma delimited.

SSMS Delimitor

You’ll need to disconnect your current session within the Object Explorer and connect back to the SQL Server.

SSMS Disconnect

Now, if we run that query and check the output file;

Export to CSV from SQL Server

Looking better now!