Home » Blog » SQL Server » How to Export Data From MS SQL Server to CSV?

How to Export Data From MS SQL Server to CSV?

Neeraj Kumar
Published By Neeraj Kumar
Debasish Pramanik
Approved By Debasish Pramanik
Published On
Last Updated

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 TaskMethod
A few rows from one queryMethod #1
Several tables with a delimiter you chooseMethod #2
A scheduled or scripted exportMethods #3 and #4
Data from a database that does not openMethod #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:

  1. Choose the query or the tables and the columns you need.
  2. Confirm that your login has SELECT permission on the source tables. Microsoft lists it for bcp exports.
  3. Decide the delimiter and the encoding that the receiving application expects.
  4. 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:

MethodBest ForLimit
SSMS Save Results AsOne query resultManual work for every query
Import and Export WizardTables and queriesMany wizard pages
bcp UtilityScripts and large tablesNo quoting of values
PowerShellScheduled exportsNeeds the SqlServer module
Recovery SoftwareData from an MDF fileTrial 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:

  1. Sign in to your instance with SSMS and open a query window.
  2. Select Results to Grid on the SQL Editor toolbar.
  3. Type your query and execute it.
  4. Right click the results grid and select Save Results As....
  5. Type a file name in the Save Grid Results dialog.
  6. 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:

  1. Expand Databases in SSMS and right click your database.
  2. Point to Tasks and select Export Data.
  3. Click Next on the welcome page.
  4. Select your SQL Server provider and the server name on Choose a Data Source.
  5. Select Flat File Destination on Choose a Destination.
  6. Type the File name, select Delimited in Format and add a quotation mark in Text qualifier.
  7. Tick Column names in the first data row when you need headers.
  8. Open Select Source Tables and Views to choose tables or Provide a Source Query to enter a query.
  9. 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:

  1. Open Command Prompt.
  2. 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 -U for a SQL login and omit -P so bcp asks 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:

  1. Install the module once with Install-Module -Name SqlServer -Scope CurrentUser.
  2. 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
  1. 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:

  1. Copy the MDF file into a separate folder.
  2. Install the software on Windows and open it. Recovery software home screen
  3. Click Open and choose the copied file from your folder. Open button MDF file selection window
  4. Select the scan mode. Quick Scan fits a healthy file and the deeper scan mode fits a damaged file. Scan mode options
  5. Keep Auto detect or choose the version that created the file. SQL Server version options
  6. Wait for the scan report. Scan report

Save the Tables as CSV

Three steps create the CSV files:

  1. Open a table in the left tree to see its recovered records. Recovered records preview
  2. Click Export and open the export options. Export button
  3. Select CSV as the export type and choose a destination folder. Export type options

Which Problems Appear After the Export?

Five problems appear in exported files:

ProblemReasonFix
Values with commas split into extra columnsThe file has no quotingUse a text qualifier in the wizard or Export-Csv
The file has no column namesbcp outputs data rows onlyUse Export-Csv or tick the header option in the wizard
Accented letters show as symbolsThe code page does not match the dataTick Unicode in the wizard or add -C 65001 to bcp
Invoke-Sqlcmd fails with a certificate errorConnections are encrypted by defaultAdd -TrustServerCertificate
The bcp file has only part of the databcp copies the first result set onlyExport 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.
  Neeraj Kumar

By Neeraj Kumar

Neeraj Kumar is a Principal Technical Architect at EmailDoctor with 19+ years of experience in email migration, cloud migration, data recovery and digital forensics. He guides enterprise teams through Exchange, Office 365 and Google Workspace projects and shares what works as an EmailDoctor writer. Every method in his posts is tested on Windows and Mac before it goes live.