Xerobit

SQL to CSV Converter

Paste SQL INSERT statements or a text-based result set to convert the extracted rows into comma-separated values. The tool uses a hand-written tokenizer that handles single-quoted strings, doubled-quote escapes, backtick identifiers, and comments. Everything runs locally in your browser so no data is uploaded.

CSV Output

id,name
1,Alice
2,Bob

How to use the sql to csv converter

  1. Paste your SQL INSERT statements or tabular text into the input area.
  2. Toggle Include column headers if you want the column names as the first row.
  3. Choose a delimiter: comma, semicolon, or tab.
  4. The CSV output updates automatically as you type.

Supported SQL formats

The converter extracts data from two common patterns. First, standard INSERT INTO table (col1, col2) VALUES (v1, v2) statements, including multiple comma-separated value tuples. Column names are taken from the parenthesised list after the table name, and each tuple becomes one CSV row.

Second, plain text result sets where columns are separated by pipes (|) or tabs. This covers the text output of command-line clients like MySQL or psql. Lines made entirely of dashes, plus signs, or pipes are treated as borders and skipped.

How the tokenizer works

Rather than relying on regular expressions that break on edge cases, the tool walks through the input character by character using a small state machine. It recognises several token types:

  • Strings: enclosed in single quotes. Doubled single quotes ('') inside a string are unescaped to a single quote.
  • Quoted identifiers: double-quoted or backtick-quoted names are read as identifiers.
  • Numbers: integers and decimals, including negative values.
  • Comments: line comments starting with -- and block comments /* ... */ are stripped before parsing.
  • Operators and punctuation: parentheses, commas, semicolons, and keywords like NULL, TRUE, and FALSE.

This approach keeps the parser tolerant of varied formatting without requiring a full SQL grammar.

Escaping rules for CSV output

When converting sql to csv, the output follows RFC 4180 rules. If a field contains the chosen delimiter, a double quote, or a newline character, the entire field is wrapped in double quotes. Any double quote inside the field is escaped by doubling it. For example, the SQL string 'He said ""hi""' becomes the CSV field "He said ""hi""".

NULL values in SQL are rendered as empty fields in the CSV output. Boolean literals TRUE and FALSE are written as-is. You can change the delimiter to semicolon or tab if your target application expects those formats instead of commas.

Limitations to keep in mind

This tool does not implement a complete SQL parser. It is designed for straightforward data extraction, not for validating queries. Complex statements such as nested subqueries inside VALUES, CTEs, or procedural blocks will not be parsed correctly. Expressions and function calls inside value lists (for example NOW() or 1 + 2) are rejected with an error instead of being guessed at, and every statement in the paste is read, not just the first.

If you need to go the other direction and turn CSV data into SQL statements, use the csv to sql insert tool. For reformatting SQL syntax itself, the sql formatter is more appropriate.

Common conversion examples

Here is how typical inputs map to outputs when converting sql to csv.

SQL InputCSV Output (with headers)
INSERT INTO t (id, val) VALUES (1, 'a');id,val
1,a
INSERT INTO t (id, val) VALUES (1, NULL), (2, 'b');id,val
1,
2,b
INSERT INTO t (msg) VALUES ('line1
line2');
msg
"line1
line2"

Notice how NULL becomes an empty field and embedded newlines cause the field to be quoted.

Worked examples

ExampleInputResult
Single INSERTSQL input: INSERT INTO users (id, name, email) VALUES (1, 'Alice', 'alice@example.com');; Include column headers: yes; Delimiter: ,id,name,email / 1,Alice,alice@example.com
Multiple value rowsSQL input: INSERT INTO products (sku, price, active) VALUES / ('A-1', 9.99, TRUE), / ('B-2', 0, FALSE);; Include column headers: yes; Delimiter: ,sku,price,active / A-1,9.99,TRUE / B-2,0,FALSE
Strings with escaped quotesSQL input: INSERT INTO t (msg) VALUES ('He said ''hello'''), ('Line1 / Line2');; Include column headers: no; Delimiter: ,He said 'hello' / "Line1 / Line2"

FAQ

Does this tool run on my server?

No. The sql to csv conversion happens entirely in your browser using JavaScript. Your SQL data is never sent over the network.

Can I convert SELECT query results?

If you paste the text output of a SELECT query (such as pipe-delimited or tab-separated results from a database client), the tool will attempt to parse it into CSV rows. It does not execute queries against a database.

How are NULL values handled?

SQL NULL literals are converted to empty fields in the CSV output, which is the standard way to represent missing values in comma-separated files.

What if my strings contain commas?

Fields containing the delimiter, double quotes, or newlines are automatically enclosed in double quotes according to RFC 4180, so commas inside data will not break the column alignment.

Why did my INSERT statement fail to parse?

The tokenizer handles standard INSERT INTO ... VALUES syntax. If your statement uses non-standard extensions, complex expressions, or deeply nested structures, simplify it or split it into basic inserts first.

Can I change the output delimiter?

Yes. Use the Delimiter dropdown to choose comma, semicolon, or tab. Semicolons are common in European spreadsheet software, and tabs produce TSV output.