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 an exact decimal (NUMERIC or DECIMAL; in SQLite only, TEXT when it has more than 15 significant digits), everything else is treated as text. Types come from all the rows, not just the first. 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. The CSV to SQL tool keeps values with leading zeros (
00501) or a leading+as text, but a ZIP code like10001looks like a number, so check any identifier column. - A column that's mostly numbers but has one row with "N/A". The tool looks at every row, so a single non-numeric value makes the whole column text rather than producing an INSERT that fails. A blank cell does not change the type; it becomes
NULL. - Dates in a non-ISO format (
03/04/2026is ambiguous — March 4th or April 3rd?) — safest to normalize toYYYY-MM-DDbefore 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). In the CSV to SQL tool, an unquoted empty field becomes NULL and a quoted "" becomes an empty string. 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're much faster for that volume. Note that server-side COPY reads the file on the database server and needs the right privileges; from your own machine use psql's \copy. MySQL's LOAD DATA LOCAL INFILE needs local_infile enabled.
-- Postgres, for large files COPY my_table (col1, col2, col3) FROM '/path/to/file.csv' WITH (FORMAT csv, HEADER true);
Convert your CSV
For anything small enough to review by eye — a one-off import, a spreadsheet someone handed you, test fixture data — the CSV to SQL tool quotes and escapes values, infers column types from every row, and gives you INSERT statements you can read before running.
Try it yourself — free, runs entirely in your browser.
Open CSV → SQL