CSV to SQL Converter
Paste a CSV or open a file and get SQL you can run right away: a CREATE TABLE with column types picked from your data, followed by INSERT statements. Choose MySQL, PostgreSQL, SQLite, or SQL Server. Everything runs in your browser, so your file is never uploaded.
What the CSV to SQL converter does
A CSV holds a table as plain text, but a database needs SQL to create that table and fill it. This tool reads your CSV, uses the header row for column names, and checks every value in each column to choose a type such as INT, DECIMAL, DATE, or VARCHAR. It then writes one INSERT per row or groups rows into bigger statements. Text is quoted and escaped the way your database expects, and empty cells become NULL.
Why use our CSV to SQL converter
- ·No upload. Files are read on your device, so customer lists and other private data stay private.
- ·SQL for your database. Quoting, escaping, types, and true/false values match MySQL, PostgreSQL, SQLite, or SQL Server.
- ·Column types from your data. Numbers, decimals, dates, and true/false are detected, and zip codes keep their leading zeros.
- ·Ready to run. Reserved words are quoted, and you can add DROP TABLE, a primary key, and a transaction.
- ·Free. No signup and no limit on how often you use it.
How to convert CSV to SQL
- 1
Paste or open your CSV
Paste CSV text into the top box, or click the folder button to open a .csv, .tsv, or .txt file. You can also drag a file onto the box. The file is read on your device and never uploaded.
- 2
Pick your database and table name
Choose MySQL, PostgreSQL, SQLite, or SQL Server, and type the table name. An opened file names the table for you, so customers.csv becomes a customers table.
- 3
Copy or download the SQL
The CREATE TABLE and INSERT statements update as you change the input or options. Copy them to your clipboard or download a .sql file, then run it in your database.
CSV to SQL INSERT example
Here is a small CSV:
id,name,zip,price,joined 1,Ada,01234,19.99,2024-01-15 2,Alan O'Brien,,5,2024-03-22
With the database set to PostgreSQL and the table namedcustomers, the tool writes:
CREATE TABLE "customers" (
"id" INTEGER,
"name" TEXT,
"zip" TEXT,
"price" NUMERIC(4, 2),
"joined" DATE
);
INSERT INTO "customers" ("id", "name", "zip", "price", "joined") VALUES
(1, 'Ada', '01234', 19.99, '2024-01-15'),
(2, 'Alan O''Brien', NULL, 5, '2024-03-22');The zip column is text because 01234 starts with a zero, the apostrophe in O'Brien is doubled, the blank zip becomes NULL, andNUMERIC(4, 2) is sized to fit the widest price. For MySQL the names would use backticks, and for SQL Server they would use brackets.
SQL column types for each database
With "Detect column types" on, the tool picks one of these types for each column. A column only gets a number, date, or true/false type when every value in it matches. Blank cells are skipped when choosing.
| Values in the column | MySQL | PostgreSQL | SQLite | SQL Server |
|---|---|---|---|---|
| Whole numbers that fit in 32 bits | INT | INTEGER | INTEGER | INT |
| Larger whole numbers | BIGINT | BIGINT | INTEGER | BIGINT |
| Decimals, like 19.99 | DECIMAL(p, s) | NUMERIC(p, s) | REAL | DECIMAL(p, s) |
| true and false | BOOLEAN | BOOLEAN | INTEGER (1 and 0) | BIT (1 and 0) |
| Dates, like 2024-01-31 | DATE | DATE | TEXT | DATE |
| Date and time, like 2024-01-31 14:05:00 | DATETIME or DATETIME(n) | TIMESTAMP | TEXT | DATETIME2 |
| Anything else | VARCHAR(255) or TEXT | TEXT | TEXT | NVARCHAR(255) or NVARCHAR(MAX) |
Text columns use VARCHAR(255) or NVARCHAR(255) when every value fits in 255 characters, and TEXT or NVARCHAR(MAX) when one is longer (MEDIUMTEXT in MySQL past 16,383 characters). MySQL gets DATETIME(n) when times have fractions of a second, so they are not cut off. Numbers with a leading zero stay as text so they are not changed, and a decimal with more than 38 digits in total is kept as text instead of being rounded.
CSV to SQL converter options
- Database
- Sets the quoting, string escaping, column types, and true/false values for MySQL (also MariaDB), PostgreSQL, SQLite, or SQL Server. Pick the one you will run the SQL in.
- Table
- The table the rows go into. Write schema.table, such as public.users or dbo.users, to use a schema. Each part is quoted on its own.
- Delimiter
- Auto detect looks at the first row and picks comma, tab, semicolon, or pipe, whichever splits it into the most fields. The detected delimiter is shown under the input.
- Rows per INSERT
- 1 writes a separate INSERT for every row. 100, 500, or 1,000 groups rows into one INSERT with many value lists, which loads much faster. SQL Server allows at most 1,000 rows in one VALUES list, so 1,000 is the largest choice.
- First row is a header
- On by default. Turn it off when your CSV has no header row, and the columns are named column1, column2, and so on.
- Create table
- Adds a CREATE TABLE statement before the inserts, with a type for each column guessed from the data. Turn it off to get only INSERT statements for a table that already exists.
- Drop table first
- Adds DROP TABLE IF EXISTS before CREATE TABLE, so you can run the script again without an error. This deletes the existing table and all of its rows.
- First column is primary key
- Marks the first column as the PRIMARY KEY. The tool warns you if that column has empty or repeated values, since the inserts would fail.
- Detect column types
- Looks at every value in a column to pick a number, date, true/false, or text type, and writes numbers without quotes. It also reads the text NULL and \N (how database exports write a missing value) as NULL. Turn it off to make every column text.
- Empty cells as NULL
- Writes empty cells as NULL. Turn it off to write an empty string ('') in text columns instead. Empty cells in number, date, and true/false columns are always NULL, since an empty string is not a valid value there.
- snake_case column names
- Turns headers like First Name or firstName into first_name, so you do not need to quote them in later queries. Accents are removed, and a name that starts with a digit gets a col_ prefix.
- Quote names
- Wraps every table and column name in the database's quotes: backticks for MySQL, brackets for SQL Server, and double quotes for PostgreSQL and SQLite. When it is off, names with spaces, symbols, or reserved words such as order and group are still quoted so the SQL runs.
- Wrap in a transaction
- Puts the whole script between BEGIN and COMMIT, so either every row is saved or none are. In SQLite this also makes large inserts much faster.
- Trim spaces
- Removes spaces at the start and end of every value and header. Spaces inside a value are kept.
Common errors when importing a CSV with SQL
These are the problems people most often hit when they turn a CSV into SQL and run it:
- ·Error near a name like order, group, or user. These are reserved words in SQL, so a column with that name has to be quoted. This tool always quotes them, even with Quote names turned off. If you write your own queries against the table later, quote those names there too.
- ·Zip codes or ids lost their leading zeros. A value like 01234 is kept as text with quotes, and its column becomes a text type, so the zero stays. If the zeros are already missing in your CSV, Excel removed them when the file was saved. Set that column to Text in Excel before saving.
- ·Dates were saved as text. Only ISO dates (2024-01-31) and date times (2024-01-31 14:05:00) are detected, because a date like 03/04/2024 means March 4 in the US and April 3 in most other countries. The tool cannot tell which one you mean, so it leaves those as text. Change the date format in your spreadsheet to YYYY-MM-DD first if you want a DATE column.
- ·MySQL says the packet is too big. A very large INSERT can go over MySQL's max_allowed_packet setting. Choose fewer rows per INSERT, or raise max_allowed_packet on the server.
- ·SQL Server says the number of row values exceeds 1000. SQL Server allows at most 1,000 rows in one INSERT ... VALUES list. The largest choice here is 1,000, so this only happens if you join statements by hand.
- ·Accented letters turn into question marks. On Windows, Excel's plain "CSV (Comma delimited)" format saves text in Windows-1252, not UTF-8. When an opened file is not valid UTF-8, this tool reads it as Windows-1252 and shows a note. For SQL Server, text values are written as N'...' so they keep non-English characters.
Other ways to import a CSV into a database
Each database has its own command for loading a CSV directly. They are faster for very large files, but the table must already exist and the file usually has to be on the server or your own machine.
| Database | Command |
|---|---|
| MySQL | LOAD DATA LOCAL INFILE 'data.csv' INTO TABLE t FIELDS TERMINATED BY ',' ENCLOSED BY '"' IGNORE 1 LINES; |
| PostgreSQL (psql) | \copy t FROM 'data.csv' WITH (FORMAT csv, HEADER) |
| SQLite | .import --csv data.csv t |
| SQL Server | BULK INSERT t FROM 'C:\data.csv' WITH (FORMAT = 'CSV', FIRSTROW = 2); |
Use this tool when you cannot reach the server's file system (common on hosted databases), when you also need the CREATE TABLE, or when you want a plain .sql file you can check and run in any SQL client.
Who converts CSV to SQL
Spreadsheets and apps export CSV, but databases are filled with SQL. These are the people who most often need to go from one to the other:
- ·Developers. Turn a spreadsheet into seed data or test fixtures for a local database, with one script that creates the table and fills it.
- ·Database admins and analysts. Load a CSV export into a table when you do not have file access on the server for LOAD DATA, COPY, or BULK INSERT, or when you want to check the data before it goes in.
- ·Students. Get a quick table to practice SQL queries on, using data from a class spreadsheet or an open data CSV.
- ·Anyone moving data between systems. Many tools export CSV but only accept SQL on the other end. A .sql file can be run in phpMyAdmin, DBeaver, pgAdmin, SQL Server Management Studio, or the sqlite3 command line.
Frequently asked questions
- How do I convert a CSV file to SQL?
- Paste your CSV into the input box or open the file with the folder button. Pick your database and table name. The tool writes a CREATE TABLE statement and INSERT statements right away. Copy the SQL or download it as a .sql file. Nothing is uploaded.
- How do I make INSERT statements from a CSV?
- Open or paste the CSV and set "Rows per INSERT". Choose 1 to get one INSERT statement per row, or 100 to 1,000 to group rows into fewer statements. Turn off "Create table" if the table already exists and you only need the inserts.
- Which databases does it support?
- MySQL (the same SQL works in MariaDB), PostgreSQL, SQLite, and SQL Server. Each one gets its own name quoting, string escaping, column types, and true/false values.
- How are column types chosen?
- The tool looks at every value in a column. If all of them are whole numbers it uses an integer type, if any have decimals it uses DECIMAL sized to fit, and ISO dates become DATE. One value that does not fit, like a word in a number column, makes the whole column text. Check the CREATE TABLE before you run it, since a small sample may not show every value the column will hold.
- How are quotes and apostrophes in my data handled?
- A single quote inside a value is doubled, so O'Brien is written as 'O''Brien', which every database reads correctly. For MySQL, backslashes are doubled too, because MySQL treats a backslash as an escape character by default.
- Why are empty cells written as NULL?
- In SQL, NULL means "no value", which is usually what a blank cell means. Turn off "Empty cells as NULL" to write an empty string in text columns instead. Number, date, and true/false columns always get NULL, because an empty string is not a valid number or date.
- Is this faster than LOAD DATA, COPY, or BULK INSERT?
- No. For files with millions of rows, your database's own import command (LOAD DATA INFILE in MySQL, COPY in PostgreSQL, .import in SQLite, BULK INSERT in SQL Server) is faster. This tool helps when you cannot use those, for example on a hosted database without file access, or when you want a script you can read, edit, and run anywhere.
- What is the largest CSV I can convert?
- Files up to 25 MB. Files over 1 MB are converted without being shown in the input box, and very large results show only the start of the SQL on screen. Copy and download always give you the full script.
- Does it handle commas and line breaks inside a field?
- Yes. A value wrapped in double quotes can contain commas, line breaks, and doubled quotes ("") that stand for a real quote. The tool follows the common RFC 4180 rules, so those values go into one SQL string.
- Is my CSV uploaded anywhere?
- No. The file is read and converted in your browser, so your data never leaves your device. The tool is free with no signup.