CodeKitHub
JSON Tools

CSV to SQL Converter

Last updated:

To turn a CSV row into SQL, each column becomes a value in an INSERT INTO statement — numbers stay unquoted, text is wrapped in single quotes with internal quotes doubled (O'Brien becomes O''Brien), and empty cells become NULL. This tool applies that conversion to your full CSV file, entirely in your browser.

What Is This Tool?

Seeding a test database, loading a spreadsheet export into a table, or migrating a small dataset often starts the same way: you have a CSV file and you need SQL. This tool parses your CSV (using the header row as column names) and builds one INSERT INTO statement covering every data row, ready to paste into a database client.

String values are wrapped in single quotes with internal quotes escaped by doubling them (so O'Brien becomes O''Brien), which is the standard escaping rule shared by MySQL, PostgreSQL and SQLite. An empty CSV cell is written as the SQL keyword NULL rather than an empty string — that's a deliberate choice, since a blank cell in a spreadsheet usually means "no value" rather than "an empty piece of text", and NULL is what most schemas expect for that.

Whether a value gets quoted or left bare is decided by a simple heuristic, not real type inference: if the trimmed cell looks like a plain integer or decimal number (optionally with a leading minus sign), it's written unquoted; everything else — including things that merely resemble numbers with extra characters, like phone numbers or ZIP codes with leading zeros — is quoted as a string. This keeps the output honest: it's a good starting point for a small import, not a substitute for validating your data against your actual table schema before running it.

Source: CSV parsing follows RFC 4180, the closest thing to a formal CSV standard; single-quote doubling for string escaping is the shared convention documented by MySQL, PostgreSQL and SQLite.

Why Use It?

  • Custom table name — set the target table once, and every row targets that table.
  • Correct string escaping — single quotes inside values are doubled so the SQL doesn't break or get truncated.
  • Predictable NULL handling — empty cells become NULL instead of an empty string, matching how most databases distinguish missing data.
  • Common SQL syntax — the INSERT INTO ... VALUES ... form generated here works unmodified in MySQL, PostgreSQL and SQLite.
  • 100% client-side — your CSV data (which may include customer records or business data) is parsed and converted in your browser and never uploaded anywhere.

How to Use

  1. Paste CSV data with a header row, e.g. name,age\nAlice,30\nBob,25.
  2. Enter the target table name (defaults to my_table).
  3. Click "Generate SQL".
  4. Copy the INSERT INTO statement or download it as a .sql file, then run it against your database.

Example

Input

name,age,city
Alice,30,
Bob,,NYC

Output

INSERT INTO `my_table` (`name`, `age`, `city`)
VALUES
  ('Alice', 30, NULL),
  ('Bob', NULL, 'NYC');

Notice the empty cells became NULL (not empty strings), and the numeric-looking age value 30 was left unquoted while every text value was quoted.

Practical tips

  • Seeding a local test database: export a small sample as CSV from a spreadsheet, convert it here, and run the INSERT statement against your dev database.
  • Always review column types before running the generated SQL against a real table — this tool infers numeric vs. text with a simple heuristic, not your actual schema, so an integer column expecting a specific format may need manual adjustment.
  • If you have thousands of rows, the single multi-row INSERT statement this tool generates is still valid SQL, but very large statements can hit a database's max packet size — split the CSV into smaller batches first if you run into that limit.

Why NULL and not an empty string

A common mistake when hand-writing CSV-to-SQL scripts is treating every missing cell as an empty string ''. That's technically valid SQL, but it usually doesn't mean what you want: a column defined as an integer will reject '' outright, and even in a text column, '' silently means something different from "we don't know this value" in most schema designs and reporting queries (a WHERE column IS NULL check won't match empty strings, and vice versa). Using NULL for blank cells matches the semantics most databases and ORMs expect, and it avoids type errors when a numeric column happens to have missing values in some rows.

Frequently Asked Questions

Does this work with every SQL database?

The generated syntax — backtick-quoted identifiers, single-quoted string values, standard INSERT INTO ... VALUES ... — is common across MySQL, PostgreSQL and SQLite. PostgreSQL doesn't strictly require backticks around identifiers (it uses double quotes if quoting is needed at all), but backtick-quoted names are harmless there too as long as you don't have unusual column names that clash with reserved words. There's no single official SQL standard being followed here — just the syntax that works unmodified across the most common databases.

Why does an empty cell become NULL instead of an empty string ''?

This is a deliberate design choice: in most real-world CSV exports, a blank cell means the value is unknown or not applicable, not literally an empty piece of text. NULL is what most database schemas expect for that. If your use case genuinely needs empty strings instead, you'll need to edit the generated SQL by hand for those cases.

How does the tool decide whether a value needs quotes?

It's a simple heuristic, not real type inference: if a trimmed cell consists only of digits (with an optional leading minus and at most one decimal point), it's written unquoted as a number. Everything else is quoted as a string. This means a ZIP code like 00501 or a phone number would be treated as text (since it fails the strict integer pattern only if it has extra characters — a leading zero alone still parses as numeric here, so double-check identifiers like ZIP codes and IDs before running the SQL).

Is my data uploaded anywhere?

No. Parsing and SQL generation both happen in JavaScript in your browser — nothing is sent to a server, which makes this safe to use with exported customer or business records.

Can it handle CSV values that contain commas or quotes?

Yes. The CSV is parsed with a proper quoted-field parser that understands commas, line breaks and doubled quotes inside quoted fields — not a naive split on commas — so a field like "Smith, John" is read as one value, not split into two columns.

Related Tools