Free tool · Developer tools
CSV to SQL converter
This CSV to SQL converter turns pasted CSV into SQL you can run. The sample of 3 rows and 5 columns becomes a CREATE TABLE and 3 INSERT statements, with O'Brien written as 'O''Brien' and the empty note as NULL.
- Free
- No sign-up
- Runs in your browser
3 rows5 columns, 3 INSERT statements, 504 bytes
- PostgreSQL
- comma delimiter
- CREATE TABLE included
- Empty cells are NULL
| Column | Type | Empty |
|---|---|---|
| id | BIGINT | 0 |
| name | VARCHAR(9) | 0 |
| joined | DATE | 0 |
| balance | DECIMAL(5, 2) | 0 |
| note | VARCHAR(23) | 1 |
How to use the CSV to SQL Converter
- Paste the CSV into Your CSV, or choose a file from your device, and type the Table name.
- Pick the Database. The page quotes names and strings the way that database expects.
- Leave Delimiter on Detect, or set it. Choose how many Rows per INSERT you want.
- Press Copy or Download .sql, and run the script in your database tool.
How do I convert a CSV file to SQL INSERT statements?
The first row of the CSV gives the column names and every other row becomes one set of values. With one row per INSERT, the second data row of the sample reads: INSERT INTO "people" ("id", "name", "joined", "balance", "note") VALUES (2, 'Li Wei', '2024-11-02', 8, NULL); The converter reads quoted values in the usual CSV way: a value in double quotes can contain the delimiter or a line break, and two quotes in a row stand for one. That is how the first note, Likes "tea", not coffee, stays in one column although it holds a comma. Rows may end with LF, CRLF or a lone CR from an old Mac file. A repeated column name gets a number, so a second a becomes a_2, and the number moves on to the next free one when a_2 is already a column of its own.
The last row of the sample comes out as INSERT INTO "people" ("id", "name", "joined", "balance", "note") VALUES (3, 'O''Brien', '2025-01-15', -4.25, 'Paid on time'); Numbers are written without quotes, text and dates are quoted, and every apostrophe is doubled so the statement stays valid. Choose 10 or 100 rows per INSERT to get one statement with several value groups, which starts with INSERT INTO "people" ("id", "n... VALUES and then lists one indented group per row.
If your file uses semicolons, tabs or pipes, the default Detect setting looks at the first line and picks the one that appears most outside quotes. A leading byte order mark, Windows line endings and blank lines are handled. The table name can be schema.table, and each part is quoted on its own.
How do PostgreSQL, MySQL, SQLite and SQL Server differ?
The differences are small, but they are the usual reason a pasted script fails. PostgreSQL, SQLite and standard SQL quote names with double quotes, MySQL uses backticks, and SQL Server uses square brackets. A quote character that appears inside a name is doubled, and in SQL Server a closing bracket is doubled. Strings use single quotes everywhere, with an apostrophe written twice.
MySQL needs one more step because a backslash begins an escape sequence in its strings unless a certain mode is on, so the page doubles every backslash for MySQL. PostgreSQL treats a backslash in an ordinary string as a plain character, so it is left alone. For SQL Server, a text value with characters outside plain ASCII gets an N before the opening quote, since a string without it is converted to the default code page of the database, which may not know the character. The table shows what each setting writes.
| Database | Name: order | Text: O'Brien | Text: C:\temp | Text: café |
|---|---|---|---|---|
| PostgreSQL | "order" | 'O''Brien' | 'C:\temp' | 'café' |
| MySQL | `order` | 'O''Brien' | 'C:\\temp' | 'café' |
| SQLite | "order" | 'O''Brien' | 'C:\temp' | 'café' |
| SQL Server | [order] | 'O''Brien' | 'C:\temp' | N'café' |
| Standard SQL | "order" | 'O''Brien' | 'C:\temp' | 'café' |
How are column types, empty cells and errors handled?
Each column gets a type from its values. Whole numbers of up to 18 digits become BIGINT, numbers with a decimal point become DECIMAL with the digits needed, dates written as YYYY-MM-DD that exist on the calendar become DATE, and everything else becomes text sized to the longest value. A value such as 00501 keeps its leading zeros, so zip is text: VARCHAR(5). The types are a starting point, so check them against your database before you run the script.
An empty cell becomes NULL by default. Switch the option off to get an empty string in text columns, while empty numeric and date cells stay NULL because an empty string is neither a number nor a date. A row with fewer values than the header is padded with NULL and the page warns about it (1 row had fewer values than the first row. The missing values are written as NULL.). A row with more values than the header is refused with the row number: "Data row 1 has 3 values but the first row has 2. Check for a stray delimiter or an unquoted value that contains it."
A quoted value that never closes gives a message with the line where it began: "A quoted value that starts on line 2 is never closed. Add the closing quote." Untick the header option if your file has no column names, and the columns are called col1, col2 and so on.
| Column | Type | Empty cells |
|---|---|---|
| id | BIGINT | 0 |
| name | VARCHAR(9) | 0 |
| joined | DATE | 0 |
| balance | DECIMAL(5, 2) | 0 |
| note | VARCHAR(23) | 1 |
Use it with
- JSON to CSV: convert JSON arrays to CSV.
- JSON Formatter: format and validate JSON.
- CSV to JSON: convert CSV to JSON.
Questions
How do I convert a CSV file to SQL?
Paste the CSV, name the table and pick the database. The page writes a CREATE TABLE and one INSERT per row, such as INSERT INTO "people" ("id", "name", "joined", "balance", "no...
How are quotes and apostrophes escaped?
An apostrophe in a value is doubled, so O'Brien is written as 'O''Brien'. MySQL also gets every backslash doubled.
Can it handle semicolons or tabs?
Yes. Detect picks the delimiter that appears most in the first line outside quotes, and you can set comma, semicolon, tab or pipe yourself.
What happens to empty cells?
They become NULL by default. With the option off, empty text cells become empty strings and empty number or date cells stay NULL.
Why is my ZIP code kept as text?
A value with a leading zero, such as 00501, would lose the zero as a number, so the column becomes VARCHAR(5).
Is my CSV uploaded?
No file is sent anywhere. The script is built by the code on this page from the text you paste or choose.
Sources
- PostgreSQL manual, Lexical Structure (quoted identifiers, string constants), read
- MySQL 8.4 reference manual, String Literals, read
- MySQL 8.4 reference manual, Schema Object Names, read
- SQLite documentation, SQL keywords and identifier quoting, read
- SQL Server documentation, Database identifiers, read
- SQL Server documentation, nchar and nvarchar (N prefix), read
The tool's results are computed in your browser by the code on this page, using the sources listed above; the explanations are checked against the tool's own output.