SQL Formatter

Format SQL queries instantly

← All guides

How to Convert a CSV File to SQL INSERT Statements

Turning a CSV export into working INSERT statements is mostly about getting quoting, types, and escaping right — here's what to check before you run the output.

A CSV export and a SQL INSERT statement are both row-oriented, which makes converting between them feel like it should be trivial — copy each row, wrap the values in parentheses, done. The details that actually cause broken imports are all in the parts people skip.

What a clean CSV needs

  • A header row with column names — this becomes your INSERT INTO table (col1, col2, ...) column list.
  • A consistent delimiter throughout the file. A stray semicolon or tab mixed in with commas silently shifts every column after it.
  • Fields containing the delimiter itself wrapped in quotes — e.g. "Smith, John" as a single value, not two columns.

Type inference is a guess, not a guarantee

Any CSV-to-SQL conversion has to guess column types from the data it sees — a column that's all digits becomes an integer, something with a decimal point becomes a float, everything else is treated as text. That guess breaks in predictable ways worth checking after conversion:

  • A phone number or ZIP code column that's "all digits" but should stay text (leading zeros get silently dropped if it's inferred as a number).
  • A column that's mostly numbers but has one row with "N/A" or a blank — that one row shouldn't force the whole column to text, but it's worth confirming it didn't.
  • Dates in a non-ISO format (03/04/2026 is ambiguous — March 4th or April 3rd?) — safest to normalize to YYYY-MM-DD before converting if the source system doesn't already.

Review the generated column types before running the output — treat them as a first draft, not the final schema.

NULL vs empty string

A genuinely empty CSV cell and a cell containing an empty string "" usually look identical in the raw file, but they should often mean different things in the database — NULL (no value) versus '' (an intentionally blank value). Check how blank cells came through in the generated statements, especially for columns where the distinction matters (a missing middle name vs. an explicitly cleared one).

Escaping — the part that causes real bugs

Any text field that might contain an apostrophe (O'Brien, it's) has to have that quote escaped inside the generated SQL string, or the statement breaks — or worse, silently produces a different statement than intended if it's hand-rolled string concatenation rather than proper escaping. This is the single most common bug in DIY CSV-to-SQL scripts, so it's worth spot-checking a few rows with apostrophes in the output before trusting the whole file.

When INSERT statements aren't the right tool

For a few hundred or thousand rows, individual INSERT statements are fine and easy to review before running. For a CSV with millions of rows, generating and executing that many individual statements is slow — that's what native bulk loaders like Postgres's COPY or MySQL's LOAD DATA INFILE exist for, and they'll import the same file an order of magnitude faster.

-- Postgres, for large files
COPY my_table (col1, col2, col3)
FROM '/path/to/file.csv'
WITH (FORMAT csv, HEADER true);

Paste your CSV below

For anything small enough to review by eye — a one-off import, a spreadsheet someone handed you, test fixture data — CSV to SQL below handles the quoting, type inference, and escaping automatically and gives you INSERT statements you can read before running.

Try it yourself — free, runs entirely in your browser.

Open CSV → SQL

Get notified when new tools launch

No spam. Just a message when something new is ready.