All tools
Free · Private · No upload

Convert CSV to SQL Online

Generate a CREATE TABLE statement and one multi-row INSERT from a CSV file, for MySQL, PostgreSQL, SQLite or SQL Server. The CSV is read in your browser and never uploaded.

Drop a CSV or Excel file here

CSV · XLSX · XLS · XLSM · XLSB

or paste your data below

How to convert CSV to SQL

Three steps from orders.csv to a .sql script.

1. Paste or drop the CSV

Paste CSV text or drop a .csv, .tsv or .txt file; the delimiter is detected.

2. Check it in the grid

Look down each column: a single non-numeric value (N/A, a dash, a unit) turns the whole column into TEXT. Fix or blank those cells if the column should be numeric.

3. Set dialect and table

Pick the dialect, type a table name, tick CREATE TABLE or DROP TABLE IF EXISTS, then copy or download data.sql.

Example: CSV in, SQL out

Two orders with a comma inside a quoted name, a zip code with a leading zero, an apostrophe and a missing total.

orders.csv
order_id,customer,zip,total
1001,"Lee, Ana",02134,49.90
1002,O'Brien,10001,
data.sql (MySQL, table "orders")
CREATE TABLE `orders` (
  `order_id` INT,
  `customer` TEXT,
  `zip` INT,
  `total` DOUBLE
);

INSERT INTO `orders` (`order_id`, `customer`, `zip`, `total`) VALUES
(1001, 'Lee, Ana', 02134, 49.90),
(1002, 'O''Brien', 10001, NULL);

zip looks numeric, so it becomes INT and 02134 is stored as 2134. Before running the script, change zip to VARCHAR(10) and put its values in quotes.

What changes between CSV and SQL

Column types are guessed

In this example order_id and zip are INT, customer is TEXT, total is DOUBLE. The rule: whole numbers give INT, a decimal point gives DOUBLE, anything else TEXT, dates included (other dialects use their own names, such as INTEGER and REAL).

Column names

Headers are turned into safe identifiers (characters other than letters, digits and _ become _; a leading digit gets a col_ prefix; duplicates get _2, _3). order_id, customer, zip and total are already valid, so they are kept as they are.

Values

The first row is inserted as (1001, 'Lee, Ana', 02134, 49.90): numbers bare, text in single quotes, with ' doubled as in 'O''Brien'. The empty total for 1002 becomes NULL. The zip 02134 is written as a bare number into an INT column, so the database stores 2134.

Detected delimiter: comma

The page counts commas, semicolons, tabs and pipes in the first line and splits on whichever appears most: 3 commas here. Quoted fields keep their delimiters and line breaks inside one cell.

When to convert CSV to SQL

CSV to SQL in JavaScript

TabularJS reads the CSV into a 2D array. Then one INSERT is built, with the same quoting rules as this page.

import tabularjs from 'tabularjs';

// CSV values are strings; the header row stays in data[0]
const { worksheets } = await tabularjs('orders.csv');
const [header, ...rows] = worksheets[0].data;

const id = (name) => '`' + String(name).trim().replace(/[^A-Za-z0-9_]/g, '_') + '`';
const value = (v) => {
  if (v == null || String(v).trim() === '') return 'NULL';
  return isNaN(v) ? `'${String(v).replace(/'/g, "''")}'` : String(v).trim();
};
console.log(`INSERT INTO ${id('orders')} (${header.map(id).join(', ')}) VALUES\n` +
  rows.map((row) => `(${header.map((_, i) => value(row[i])).join(', ')})`).join(',\n') + ';');

TabularJS returns CSV values as strings and leaves the header in data[0]. It does not guess the delimiter: comma is the default, so pass { delimiter: ';' } for semicolon files.

npm install tabularjs MIT licensed

Frequently asked questions

Is this faster than LOAD DATA INFILE or COPY?

No. For large files the database's bulk loader (MySQL LOAD DATA, PostgreSQL COPY, SQLite .import) is faster. This page is for when you need a portable .sql script, or have no file access on the server.

Does the script work in PostgreSQL?

Yes. Choose PostgreSQL: identifiers are double-quoted, the types become INTEGER, REAL and TEXT, and the script runs in psql or any client.

What if my CSV has no header row?

Untick "First row as header". The first line is then treated as data and the columns are named col_1, col_2, col_3 in the SQL.

Which databases does the SQL run on?

MySQL and MariaDB (backtick quoting), PostgreSQL and SQLite (double-quote quoting) and SQL Server (bracket quoting). Pick the dialect so identifiers and column types match your database.

Does orders.csv get uploaded when I convert it to SQL?

No. The CSV is read in your browser and never uploaded.