How to use the csv to sql insert converter
- Paste your CSV data into the text area.
- Enter the target table name.
- Choose the delimiter, header option, quote character, and NULL representation.
- The SQL INSERT statements appear instantly in the result box.
- Copy the statements and run them in your database.
How the CSV parser works
The tool implements a small RFC 4180 compliant parser written in TypeScript. It scans the input character by character, tracking whether it is inside a quoted field. When a quote character is encountered, the parser toggles the in-quote state unless the quote is doubled, in which case a literal quote is added to the field. Delimiters and line breaks are only treated as separators when outside quotes. This approach correctly handles fields that contain commas, semicolons, tabs, pipes, and even newlines. The parser also normalises CRLF (\r\n) and lone LF (\n) line endings.
For example, the CSV line 1,"Item, with comma",30 is split into three fields: 1, Item, with comma, and 30. The comma inside the quotes is not a delimiter. A field with an embedded newline like "Line1\nLine2" remains a single field. This csv to sql insert conversion is reliable for messy real-world data.
Generating the INSERT statements
After parsing, the first row is used as column names if the header option is checked. Otherwise columns are named col1, col2, etc. Each subsequent row becomes an INSERT INTO table (cols) VALUES (vals); statement. String values are wrapped in single quotes and any single quote inside the value is escaped by doubling it (the standard SQL escape). Empty fields are rendered as the NULL representation you provide (default NULL) without quotes. If a row has a different number of fields than the header, a warning comment is inserted before the statement, e.g. -- Warning: row 3 has 5 columns, expected 4. This makes ragged data visible without breaking the SQL.
Common use cases
- Loading exported spreadsheets into a relational database.
- Creating seed data for development or test environments.
- Migrating data from a CSV-only system to SQL.
- Generating bulk insert scripts for ETL pipelines.
Because the conversion happens in the browser, you can process sensitive data without sending it to a server. The csv to sql insert workflow is ideal for quick data imports.
Reference table of delimiter options
| Delimiter | Typical use |
|---|---|
| Comma (,) | Standard CSV, Excel export |
| Semicolon (;) | European CSV where comma is decimal separator |
| Tab | TSV files, copy-paste from spreadsheets |
| Pipe (|) | Custom exports, log files |
Edge cases and limitations
The parser does not support escaped characters like \\n inside quoted fields; it treats a literal backslash as a normal character. Very large files (tens of megabytes) may cause the UI to lag because the whole text is processed in memory. The tool does not generate CREATE TABLE statements; for that see the SQL Create Table Generator. If you need to escape identifiers (table or column names) you must do it manually after copying the output. Also note that the parser assumes the quote character is a single character; multi-character quotes are not supported.
Worked examples
| Example | Input | Result |
|---|---|---|
| Simple CSV with header | CSV data: id,name,age / 1,Alice,30 / 2,Bob,25; Table name: users; Delimiter: ,; First row is header: yes; Quote character: "; NULL representation: NULL | INSERT INTO `users` (`id`, `name`, `age`) VALUES ('1', 'Alice', '30'); / INSERT INTO `users` (`id`, `name`, `age`) VALUES ('2', 'Bob', '25'); |
| Quoted fields with commas and newlines | CSV data: id,description / 1,"Item, with comma" / 2,"Item with / newline"; Table name: items; Delimiter: ,; First row is header: yes; Quote character: "; NULL representation: NULL | INSERT INTO `items` (`id`, `description`) VALUES ('1', 'Item, with comma'); / INSERT INTO `items` (`id`, `description`) VALUES ('2', 'Item with / newline'); |
| No header, semicolon delimiter | CSV data: 1;Alice;30 / 2;Bob;25; Table name: people; Delimiter: ;; First row is header: no; Quote character: "; NULL representation: NULL | INSERT INTO `people` (`col1`, `col2`, `col3`) VALUES ('1', 'Alice', '30'); / INSERT INTO `people` (`col1`, `col2`, `col3`) VALUES ('2', 'Bob', '25'); |
| Ragged rows (warning comments) | CSV data: a,b,c / 1,2 / 3,4,5,6; Table name: test; Delimiter: ,; First row is header: yes; Quote character: "; NULL representation: NULL | -- Warning: row 2 has 2 columns, expected 3 / INSERT INTO `test` (`a`, `b`, `c`) VALUES ('1', '2', NULL); / -- Warning: row 3 has 4 columns, expected 3 / INSERT INTO `test` (`a`, `b`, `c`) VALUES ('3', '4', '5'); |
FAQ
Does the tool send my CSV data to a server?
No. All parsing and SQL generation run in your browser. Nothing is uploaded.
Can I use a different quote character, like a single quote?
Yes. Choose the quote character from the dropdown. The parser will treat that character as the quoting delimiter.
What happens if a row has more columns than the header?
A warning comment is added before the INSERT statement, and only the first N columns (where N is the header count) are used. Extra columns are ignored.
How are empty fields handled?
Empty fields become the NULL representation you set (default NULL). If you leave the NULL representation blank, empty fields become empty strings ('').
Can I convert TSV files?
Yes. Select Tab as the delimiter.
Is there a limit on the number of rows?
There is no hard limit, but very large inputs may slow down the browser. For huge files consider a command-line tool.
Does this csv to sql insert converter support PostgreSQL or MySQL syntax?
The output uses standard SQL with backtick-quoted identifiers, which works for MySQL and MariaDB. For PostgreSQL, replace backticks with double quotes or remove them. Values are single-quoted with embedded quotes doubled. MySQL by default also treats a backslash inside a string as an escape character, so review data containing backslashes before running it. Blank lines are skipped, and an unclosed quote returns an error.