Export data from SQL Server to CSV via command line on Windows

Want to export SQL Server data to CSV file via Command Line Interface (CLI) on Windows?

Using mssql-cmd, Command Line Interface (CLI) client for SQL Server, you can unload SQL Server data to CSV/TSV/TXT easily and fast. Direct Interaction or Scripting and Automation.

  • No need to install SQL Server client library.
  • Convenient to deploy to cloud. No need to run installation file, just uncompress and run, immediately.
  • Commands are entered into a terminal or command prompt, accessed from local or remote (like SSH).
  • Support Windows, Linux, and MacOS.

Download mssql-cmd Windows version Download mssql-cmd

Export SQL Server data to CSV/TSV/TXT via command line on Windows

1. Unload Table.

1) In interactive mode:

mssql-cmd> unload
      > table=media_messages 
      > datafile=/data/media_messages.csv 
      > delimiter=, 
      > quote=" 
      > header=y;

2) In batch mode:

mssql-cmd dsn="pan:withdata@192.168.0.102/test" task=unload table=media_messages datafile=/data/media_messages.csv delimiter=, quote=" header=y quit=y

2. Unload Query.

1) In interactive mode:

mssql-cmd> unload
      > query="select * from media_messages where id > 100"
      > datafile=/data/media_messages.csv 
      > delimiter=, 
      > quote=" 
      > header=y;

2) In batch mode:

mssql-cmd dsn="pan:withdata@192.168.0.102/test" task=unload query="select * from media_messages where id > 100" datafile=/data/media_messages.csv delimiter=, quote=" header=y quit=y

3. Unload All Tables.

1) In interactive mode:

mssql-cmd> unload
      > table=all 
      > folder=/data/tables_data
      > delimiter=, 
      > quote=" 
      > header=y;

2) In batch mode:

mssql-cmd dsn="pan:withdata@192.168.0.102/test" task=unload table=all folder=/data/tables_data delimiter=, quote=" header=y quit=y

See parameters detail.


More about mssql-cmd – Command Line Interface (CLI) tool for SQL Server

Download mssql-cmd Windows versionDownload mssql-cmd

SQL Server → CSV reference

Type mapping

Value kindin SQL Serverin CSV
Intint1234
Bigintbigint9007199254740993
Decimaldecimal(p,s)1234.56
Floatfloat1.234e+10
Textnvarchar(n)quoted only when it contains a comma or a quote
Long textnvarchar(max)must be quoted; line breaks inside force multi-line records
Datedate2026-09-18
Datetimedatetime22026-09-18 14:30:00
Binaryvarbinary(max)not representable - base64 or hex string
BooleanbitTRUE / FALSE, or 1 / 0 depending on the writer
JSONnvarchar(max)the whole JSON value quoted, with internal quotes doubled

Dialect differences

SQL Server is a database server and CSV is a file format, so the gap is wider than a dialect difference – the rows below are where values and structure stop lining up.

Propertyin SQL Serverin CSV
port1433—
identifiers[square brackets]Double quotes; a literal quote is doubled ("" )
row limitSELECT TOP n—
datesdatetime, datetime2, date, timeConventional text: 2026-09-18 or 2026-09-18 14:30:00
long textnvarchar(max)—
binaryvarbinary(max)Not supported
separator—Comma ","
encoding—No declaration - UTF-8 works only with a BOM
typing—None - every field is text
shapeTables of rows and columnsFlat table, optional header row
ceiling—No formal limit; row count bounded by memory

Command line

SideCommand
SQL Serverbcp
SQL Server (load)bcp, or BULK INSERT

Convert your data online, free

Free in-browser converters for CSV, Excel, JSON, XML and SQL: Withdata Online Data Conversion. Large files, batch conversion or scheduling? See the offline desktop apps, or browse all data conversion tools.


Posted

in

by