Export data from MS SQL Server to CSV with SSMS, the Import and Export Wizard, bcp or PowerShell. SSMS and the wizard suit small exports. bcp and PowerShell suit scripts and large tables. Recovery software exports a CSV file from an MDF file when the instance does not open the database.
Your manager asks you to export data from MS SQL Server to CSV and the file must open cleanly in Excel. Four built in tools can do it and they handle commas, headers and Unicode text differently.
Most guides stop at the Export Wizard. Few explain the delimiter, the quoting and the encoding.
This guide explains five ways to export data from MS SQL Server to CSV and what every method does with commas and Unicode text.
Microsoft documents the command line options in its bcp utility reference.
Which Method Fits Your Export?
Match your task to a method first:
| Your Task | Method |
|---|---|
| A few rows from one query | Method #1 |
| Several tables with a delimiter you choose | Method #2 |
| A scheduled or scripted export | Methods #3 and #4 |
| Data from a database that does not open | Method #5 |
Flat files from the Import and Export Wizard come as Delimited, Fixed width or Ragged right according to Microsoft. Select Delimited for a CSV file.
What Should You Check Before You Export?
Four checks prevent a broken file:
- Choose the query or the tables and the columns you need.
- Confirm that your login has
SELECTpermission on the source tables. Microsoft lists it forbcpexports. - Decide the delimiter and the encoding that the receiving application expects.
- Note whether the data has commas, line breaks or non English text because those need quoting and Unicode.
5 Ways to Export Data From MS SQL Server to CSV
Methods #1 to #4 use built in tools and Method #5 uses software:
| Method | Best For | Limit |
|---|---|---|
| SSMS Save Results As | One query result | Manual work for every query |
| Import and Export Wizard | Tables and queries | Many wizard pages |
| bcp Utility | Scripts and large tables | No quoting of values |
| PowerShell | Scheduled exports | Needs the SqlServer module |
| Recovery Software | Data from an MDF file | Trial exports are capped |
Method #1: Save Query Results as CSV in SSMS (Free)
SSMS saves a result grid as a CSV file in six steps:
- Sign in to your instance with SSMS and open a query window.
- Select Results to Grid on the SQL Editor toolbar.
- Type your query and execute it.
- Right click the results grid and select Save Results As....
- Type a file name in the Save Grid Results dialog.
- Select CSV in Save as type and click Save.
Note: SSMS 22.3.3 and earlier export CSV and TXT files. SSMS 22.4.1 adds JSON, XML, Excel and Markdown according to Microsoft. Change the default folder under Tools, Options, Query Results, SQL Server, General, Query results directory.
Results to File saves the output without the grid and uses the .rpt extension by default. Rename the file or change Save as type.
Method #2: Export Tables With the Import and Export Wizard (Free)
Export tables or query results to a flat file with the wizard. Nine steps create the CSV file:
- Expand Databases in SSMS and right click your database.
- Point to Tasks and select Export Data.
- Click Next on the welcome page.
- Select your SQL Server provider and the server name on Choose a Data Source.
- Select Flat File Destination on Choose a Destination.
- Type the File name, select Delimited in Format and add a quotation mark in Text qualifier.
- Tick Column names in the first data row when you need headers.
- Open Select Source Tables and Views to choose tables or Provide a Source Query to enter a query.
- Confirm the comma as the column delimiter and click Finish on Save and Run Package.
Note: Tick Unicode for text in several languages. Microsoft states that Unicode and Code page cannot work together. Destinations with only a 32 bit provider do not appear in the 64 bit wizard.
Method #3: Export With the bcp Utility (Free)
bcp copies a query result to a text file from the command line. Two steps export it:
- Open Command Prompt.
- Type the command with your own server, database and query:
bcp "SELECT OrderID, CustomerName, OrderDate FROM dbo.Orders" queryout C:\temp\orders.csv -c -t, -S SQLHOST01 -d SalesDB -T
Five options do the work:
- queryout. Copies data from a query according to Microsoft.
- -c. Uses character format with a tab as the default field separator.
- -t, Sets a comma as the field terminator.
- -S and -d. Name the server and the database.
- -T. Connects with your Windows credentials. Use
-Ufor a SQL login and omit-Psobcpasks for the password.
Note: Microsoft states that bcp copies only the first result set of a query. Add -C 65001 for UTF-8 on SQL Server 2016 and later. bcp outputs data rows only so add a header row in the query when you need one.
Method #4: Export With PowerShell (Free)
Invoke-Sqlcmd sends the query and Export-Csv creates the file. Three steps export it:
- Install the module once with
Install-Module -Name SqlServer -Scope CurrentUser. - Type the script with your own server, database and query:
Invoke-Sqlcmd -ServerInstance "SQLHOST01" -Database "SalesDB" -Query "SELECT OrderID, CustomerName, OrderDate FROM dbo.Orders" |
Select-Object OrderID, CustomerName, OrderDate |
Export-Csv -Path C:\temp\orders.csv -NoTypeInformation
- Open the CSV file and check the header row.
Note: Export-Csv adds the column names as the first row and quotes every field by default. Add -UseQuotes AsNeeded to quote only fields with a delimiter, a quotation mark or a line break. Without -Append the command replaces an existing file without warning.
Microsoft states that module version 22 and later encrypts connections by default. Add -TrustServerCertificate when the certificate chain is not trusted. Could Not Open a Connection to SQL Server Error 40 lists other connection failures.
Method #5: Export CSV Files From an MDF File With EmailDoctor (Trial Limit Applies)
Methods #1 to #4 need an instance that opens your database. EmailDoctor SQL Database Recovery exports tables from your copied MDF file to CSV files when the instance refuses to start or the database is damaged. Demo mode exports 5 other objects and up to 25 records of a table.
Add the MDF File
Stop the SQL Server service before copying because an active instance uses the database files. Six steps add and scan the copy:
- Copy the MDF file into a separate folder.
- Install the software on Windows and open it.
- Click Open and choose the copied file from your folder.
- Select the scan mode. Quick Scan fits a healthy file and the deeper scan mode fits a damaged file.
- Keep Auto detect or choose the version that created the file.
- Wait for the scan report.
Save the Tables as CSV
Three steps create the CSV files:
- Open a table in the left tree to see its recovered records.
- Click Export and open the export options.
- Select CSV as the export type and choose a destination folder.
Which Problems Appear After the Export?
Five problems appear in exported files:
| Problem | Reason | Fix |
|---|---|---|
| Values with commas split into extra columns | The file has no quoting | Use a text qualifier in the wizard or Export-Csv |
| The file has no column names | bcp outputs data rows only | Use Export-Csv or tick the header option in the wizard |
| Accented letters show as symbols | The code page does not match the data | Tick Unicode in the wizard or add -C 65001 to bcp |
| Invoke-Sqlcmd fails with a certificate error | Connections are encrypted by default | Add -TrustServerCertificate |
The bcp file has only part of the data | bcp copies the first result set only | Export one query per file |
Frequently Asked Questions
Can I Export Data to CSV Without SSMS?
Yes. bcp and PowerShell both export from the command line. Neither needs Management Studio.
Why Does the bcp File Have No Column Names?
bcp saves the rows of your query and no header. Add the column names to the query or use Export-Csv.
Why Does bcp Separate Values With Tabs?
Tab is the default field terminator according to Microsoft. Add -t, to the command for commas.
Which Permission Does a bcp Export Need?
SELECT permission on the source table. Microsoft states that bcp out needs no other permission on the table.
What Does Export-Csv Do When the File Exists?
Export-Csv replaces the contents without warning unless you add -Append. Use a new file name to keep the old file.
Can SSMS Save Results as Excel?
Only in SSMS 22.4.1 and later. SSMS 22.3.3 and earlier export CSV and TXT files.
Why Does the Wizard Show Fewer Destinations?
Destinations with only a 32 bit provider do not appear in the 64 bit wizard. Start the 32 bit wizard to see them.
Can I Try the Recovery Software for Free?
Yes. Demo mode exports 5 other objects and up to 25 records of a table until you buy a license.
Author’s Verdict
Use SSMS for one query and the wizard for several tables. Script the export with bcp or PowerShell when it repeats.
Check the first rows of every file for headers and Unicode text before you send it. Quote values that contain commas.
Use recovery software when the database does not open and the data is due.
- SSMS and the Import and Export Wizard export small data sets to CSV.
- bcp and PowerShell export scripts and large tables.
- Recovery software exports CSV files from your MDF file copy when the instance does not open the database.