Convert CSV to SQL INSERT statements

Turn CSV into CREATE TABLE and INSERT statements for MySQL, PostgreSQL, SQLite or SQL Server, with guessed column types.

How it works

  1. Paste your CSV
  2. Pick the SQL dialect and table name
  3. Copy or download the SQL

What the tool writes

To convert CSV to SQL, paste your CSV, choose MySQL, PostgreSQL, SQLite or SQL Server, and the tool writes a CREATE TABLE statement with guessed column types followed by INSERT statements for every row. The first row is the header by default and gives the column names; turn that off to get column1, column2 and so on. The separator is detected (comma, semicolon, tab or pipe) or chosen by you. The sample, a header and three rows, becomes in MySQL: CREATE TABLE `my_table` with id INT, name VARCHAR(255), city VARCHAR(255), score DOUBLE and joined DATE, then INSERT INTO `my_table` (`id`, `name`, `city`, `score`, `joined`) VALUES (1, 'Ann', 'Pune', 7.5, '2024-01-15'), and two more rows. The quoted name O'Brien, Bob keeps its comma and becomes 'O''Brien, Bob'.

How column types are guessed

Every value in a column is looked at. If all non-empty values are whole numbers of up to 15 digits with no leading zero the column is INT, or BIGINT when one is beyond plus or minus 2,147,483,647. Decimals such as 7.5, .5 or 1e5 make a float column (DOUBLE, DOUBLE PRECISION, REAL or FLOAT); a column of whole numbers and decimals is a float column. true and false give BOOLEAN, or 1 and 0 in SQLite and SQL Server. A valid calendar date written YYYY-MM-DD is DATE, and YYYY-MM-DD HH:MM:SS (a T instead of the space is fine) is DATETIME or TIMESTAMP. Anything else, including values like 007, +91, 1,000 and dates with a time zone, is text: VARCHAR(255) or TEXT in MySQL, NVARCHAR in SQL Server and TEXT elsewhere. SQLite has no date type, so dates are TEXT there. A mixed column falls back to text. Turn off Guess column types to make every column text.

Quoting and escaping

Table and column names are always quoted in the dialect's style: backticks for MySQL, double quotes for PostgreSQL and SQLite, square brackets for SQL Server, with the quote character doubled if the name contains it. Write schema.table to qualify the table. In text values a single quote is doubled. MySQL treats a backslash as an escape character unless the NO_BACKSLASH_ESCAPES mode is on, so the tool doubles backslashes for MySQL; if your server uses that mode, turn on MySQL NO_BACKSLASH_ESCAPES mode and backslashes are written as they are. In PostgreSQL a text value that contains a backslash is written as an E'...' literal with the backslash doubled, which reads the same whether standard_conforming_strings is on or off. SQL Server text gets an N prefix so Unicode survives. Numbers are written only when they match a strict number pattern, never as raw text. NUL characters are removed. Empty values become NULL; typed columns always use NULL, and with the NULL option off an empty text value becomes an empty string.

Batches, limits and what it does not do

Rows per INSERT is 100 by default and can be 1 to 1,000. SQL Server allows at most 1,000 rows in one INSERT ... VALUES statement, which is why 1,000 is the top. A row with fewer values than the longest row is filled with NULL, and the tool says how many rows that happened to. It accepts up to 1,000,000 characters and 500 columns, and refuses an unclosed quote with the line number. It adds no primary key, index or constraint, does not run the SQL, and guesses types only from the rows you paste, so check the types before using them on a larger data set. The work happens in your browser; the CSV is not uploaded or stored, only your option choices are remembered.

Frequently asked questions

How do I convert a CSV file to SQL INSERT statements?

Paste the CSV, pick the dialect, set the table name and copy the result. A CSV with a header and 3 rows gives one CREATE TABLE and one INSERT with 3 rows. Use Download .sql to save it, then run it in your database client.

How does it decide the column types?

From the values. A column of whole numbers is INT, numbers with a decimal point are DOUBLE, true and false are BOOLEAN, YYYY-MM-DD dates are DATE, and everything else is text. One text value in a numeric column makes the whole column text.

Why are my ID or phone number columns text?

A value with a leading zero such as 00123, a plus sign such as +9198, or more than 15 digits would lose information as a number, so it stays text. A column with even one such value becomes text, and the zeros are kept.

How are quotes and backslashes escaped?

A single quote is doubled in every dialect, so O'Brien becomes 'O''Brien'. A backslash is doubled for MySQL only, because MySQL reads it as an escape; in PostgreSQL, SQLite and SQL Server it is left as it is.

How many rows go in one INSERT?

100 by default, and you can set 1 to 1,000. A CSV of 250 rows gives 3 INSERT statements at the default. SQL Server rejects more than 1,000 rows in one VALUES list, so the setting stops at 1,000.

Is my CSV uploaded?

No. The CSV is processed in your browser, in this tab, and no server receives it. Your option choices are saved in this browser; the CSV is not.