CSV to SQL Converter
Convert CSV to SQL INSERT statements, with an optional CREATE TABLE, for MySQL, PostgreSQL and SQLite. Keeps leading zeros, escapes MySQL backslashes, handles quoted fields and NULLs.
- Runs in your browser
- Tool input is not uploaded to ZeroTool servers
- Free · No Sign-Up
Scan with WeChat to share this tool
Examples, details and FAQ Worked examples, how it compares with other tools, and answers to common questions.
How Values Are Written
- Empty CSV fields produce
NULL. - A column whose non-empty cells are all plain numbers (
42,-7,19.90) is written without quotes. A plain number has no leading zero, no+, no exponent and at most 15 digits. - Every other column is written as quoted strings, including its numbers, so
02134,1e5and a 20-digit card number keep their exact text. MySQL, PostgreSQL and SQLite all convert a quoted number when the target column is numeric. - Single quotes are doubled (
O''Brien) in every dialect. In MySQL, backslashes are doubled too: MySQL's default SQL mode reads\ninside a string as a newline (MySQL 8.4 Reference Manual, String Literals, Table 11.1). PostgreSQL withstandard_conforming_stringson (the default since 9.1, PostgreSQL 4.1.2.1) and SQLite store backslashes as written. - Cells beyond the header get extra columns named
column_3,column_4and so on, and the status line lists them. Short rows are padded withNULL.
Example: Quotes, Leading Zeros and Empty Fields
Input (SQLite, batch mode, table customers):
id,name,zip,note
1,"O'Brien, Pat",02134,
2,Lee,10001,"path C:\new"
INSERT INTO "customers" ("id", "name", "zip", "note") VALUES
(1, 'O''Brien, Pat', '02134', NULL),
(2, 'Lee', '10001', 'path C:\new');
The quoted field "O'Brien, Pat" keeps its comma. The empty note becomes NULL. 02134 makes the zip column a text column, so 10001 is quoted as well. Run against SQLite 3, SELECT zip FROM customers returns 02134 and 10001.
The same input in MySQL doubles the backslash, so the stored value is still path C:\new:
id,name,zip,note
1,"O'Brien, Pat",02134,
2,Lee,10001,"path C:\new"
INSERT INTO `customers` (`id`, `name`, `zip`, `note`) VALUES
(1, 'O''Brien, Pat', '02134', NULL),
(2, 'Lee', '10001', 'path C:\\new');
If your server runs with NO_BACKSLASH_ESCAPES in sql_mode, backslashes are ordinary characters; use the PostgreSQL or SQLite output for that server instead.
Example: CREATE TABLE
With Add CREATE TABLE ticked, column types are chosen from the values: whole numbers become INTEGER (INT in MySQL, BIGINT beyond 2,147,483,647), numbers with a decimal point become DOUBLE PRECISION (DOUBLE in MySQL, REAL in SQLite), everything else TEXT. PostgreSQL:
order_id,amount,paid_at
1001,19.90,2026-09-30
1002,5,
CREATE TABLE "orders" ("order_id" INTEGER, "amount" DOUBLE PRECISION, "paid_at" TEXT);
INSERT INTO "orders" ("order_id", "amount", "paid_at") VALUES
(1001, 19.90, '2026-09-30'),
(1002, 5, NULL);
Change the types before running it when they matter: money belongs in NUMERIC / DECIMAL, and dates such as paid_at in DATE or TIMESTAMP. No primary key or constraints are added.
Example: More Cells Than Headers
sku,qty
A-1,3,fragile
B-2,5
INSERT INTO "stock" ("sku", "qty", "column_3") VALUES ('A-1', 3, 'fragile');
INSERT INTO "stock" ("sku", "qty", "column_3") VALUES ('B-2', 5, NULL);
The status line reports the added column column_3. Rename it in the header row if you want a real name.
Example: Repeated Header Names and Stray Quotes
A header row from a spreadsheet can repeat a name or leave the last cell empty. Every column still gets its own name (SQLite, Add CREATE TABLE, table stock):
sku,qty,qty,
A-1,3,4,boxed
B-2,5,,
CREATE TABLE "stock" ("sku" TEXT, "qty" INTEGER, "qty_2" INTEGER, "column_4" TEXT);
INSERT INTO "stock" ("sku", "qty", "qty_2", "column_4") VALUES
('A-1', 3, 4, 'boxed'),
('B-2', 5, NULL, NULL);
The status line lists the new names:
Duplicate or empty header names were renamed: qty → qty_2, "" → column_4.
The second qty becomes qty_2, and the empty fourth header becomes column_4 (its position). Names are compared without case, so Name and name also count as the same name: MySQL column names are not case-sensitive (MySQL 8.4, Identifier Case Sensitivity), and SQLite rejects "Name" and "name" in one table. Quoted names are case-sensitive in PostgreSQL, but the tool renames them in the same way, so the three dialects get the same column names. A new name never takes a name that is already in the header: a,a,a_2 gives a, a_3, a_2.
item,size
monitor,27" wide
CREATE TABLE "stock" ("item" TEXT, "size" TEXT);
INSERT INTO "stock" ("item", "size") VALUES
('monitor', '27" wide');
Only a double quote at the start of a field opens a quoted field. A quote inside an unquoted field, like the inch mark in 27" wide, is an ordinary character. A quoted field that is never closed stops the conversion:
sku,note
A-1,"fragile
B-2,ok
A quoted field that starts on line 2 is never closed. Add the closing " or remove the opening one.
The line number counts the lines in CSV Input, blank lines at the top included. The SQL output stays empty until the quote is fixed.
Limits
- The delimiter is a comma, and the first row is always the header. Semicolon or tab separated files need converting first.
- Types are guessed from the text only. Dates, booleans and
1e5are strings. - Table and column names are quoted as single identifiers, so
public.usersbecomes one name with a dot in it. - Large files are converted in the browser tab in one pass; for millions of rows use the database's bulk loader (
LOAD DATA,COPY,.import). - Upload CSV reads UTF-8 files only. A file in another encoding is not loaded, and the status line gives the first byte that is not UTF-8 (counting from 0); a UTF-16 file is reported as UTF-16, and a binary file by its first NUL byte.
- A quoted field that is never closed stops the conversion with its line number; a quote inside an unquoted field is kept as text.
- Repeated header names (compared without case) get
_2,_3, and empty header cells getcolumn_N; the status line lists them. Other names are used as written, spaces included: in MySQL a column name cannot end with a space, and characters outside the Basic Multilingual Plane, such as emoji, are not allowed in names (MySQL 8.4 Reference Manual, Schema Object Names). - Only a byte order mark, blank lines at the top and line breaks at the end are removed from the input. Spaces at the start or end of a header or a cell are kept as they are.
FAQ
What does this tool do?
Converts CSV data into SQL INSERT statements, and optionally a CREATE TABLE statement. The header row gives the column names; every data row becomes a row of values, quoted and escaped for the chosen dialect.
Which SQL dialects are supported?
MySQL (backtick identifiers, backslashes in strings doubled), PostgreSQL and SQLite (double-quote identifiers, backslashes written as they are). The INSERT syntax is the same in all three.
How are NULL values handled?
Empty fields become NULL (without quotes), and so does a quoted empty field (""). A field that contains only spaces is a string, not NULL. The tool cannot produce an empty string ''.
Why is 02134 quoted but 10001 sometimes not?
A column is written as numbers only when every non-empty cell in it is a plain number: no leading zeros, no plus sign or exponent, at most 15 digits. 02134 is not, so the whole zip column is quoted and the leading zero survives.
Why is my uploaded CSV file not loaded?
Upload CSV reads UTF-8 only. A file in another encoding (Shift_JIS, GBK, EUC-KR, Windows-1252) is not loaded, and the status line gives the first byte that is not UTF-8. A UTF-16 file is reported as UTF-16, and a binary file by the position of its first NUL byte. In each case CSV Input keeps its text; save the file as UTF-8 and load it again. A UTF-8 byte order mark at the start is removed.
Is any data sent to a server?
No. The CSV is converted in this browser tab; the CSV and the SQL are not sent anywhere and are not saved in browser storage. The page's analytics records one usage event (the tool name and the action convert) when you finish editing the CSV or the table name, change an option, or load a file. The event does not contain the CSV or the SQL.