Map Extract
Free tool

Convert CSV to SQL INSERT statements

Paste a CSV whose first row is the column names, choose your database, and get a CREATE TABLE statement plus INSERT statements batched to the size you set. Integer and decimal columns are detected, text is quoted and escaped, and empty cells become NULL.

Runs in your browser. Nothing you enter is uploaded.

Generating SQL

  1. Paste the CSV

    The first row must hold the column names. They are quoted as identifiers for your database, so spaces and capitals survive.

  2. Name the table

    Type the Table name; the default is businesses.

  3. Pick the database

    PostgreSQL, MySQL / MariaDB, SQLite or SQL Server. This sets the identifier quotes and the column types used.

  4. Set the batch size and options

    Rows per INSERT groups rows into multi-row statements (500 by default, up to 1,000). Include CREATE TABLE adds the table definition; Detect number columns types numeric columns instead of quoting them.

  5. Copy or download

    Copy the SQL or save data.sql and run it in your database client.

Two stockists into PostgreSQL

A small CSV with an apostrophe in a business name, a missing review count and a decimal rating.

With the PostgreSQL defaults, reviews becomes INTEGER and rating NUMERIC because every value in those columns is a number, and the text columns become VARCHAR(50). The apostrophe in O'Neill is doubled, which is how SQL escapes it, and the empty cell is written as NULL rather than an empty string.

Input
business,town,reviews,rating
Kiln & Co Pottery,Frome,48,4.8
O'Neill Plumbing,York,,4.5
Result
CREATE TABLE "stockists" (
  "business" VARCHAR(50),
  "town" VARCHAR(50),
  "reviews" INTEGER,
  "rating" NUMERIC
);

INSERT INTO "stockists" ("business", "town", "reviews", "rating") VALUES
  ('Kiln & Co Pottery', 'Frome', 48, 4.8),
  ('O''Neill Plumbing', 'York', NULL, 4.5);

What changes by database

PostgreSQLMySQL / MariaDBSQLiteSQL Server
Identifier quotes"name"name"name"[name]
Short textVARCHAR(n)VARCHAR(n)TEXTNVARCHAR(n)
Text over 255 charactersTEXTTEXTTEXTNVARCHAR(MAX)
Decimal numbersNUMERICNUMERICREALNUMERIC
Whole numbersINTEGERINTEGERINTEGERINTEGER

How types are chosen

A column is INTEGER only if every non-empty value is a whole number of up to ten digits without a leading zero. That keeps phone numbers, postcodes and company numbers such as 09876543 as text, where they belong. A column is NUMERIC (REAL in SQLite) if every value is a number, decimals allowed. Anything else is text, sized to the longest value rounded up to the next 50 characters, up to 255.

The generated types are a starting point. If you will add longer values later, widen the VARCHAR, and add primary keys, indexes and constraints yourself; the converter does not guess them. Untick Detect number columns and every value is quoted, which is safer when loading into an existing table with its own types.

Questions people ask

How do I import a CSV file into a SQL database?

Paste the CSV here, choose your database and run the generated SQL in your client, such as psql, MySQL Workbench, DB Browser for SQLite or SQL Server Management Studio. For very large files, your database's own bulk loader, such as COPY in PostgreSQL or LOAD DATA in MySQL, is faster than INSERT statements.

Why are my phone numbers stored as text?

Values with a leading zero or spaces are not treated as numbers, because storing 01632960410 as an integer would drop the zero and change the data. Text is the right type for phone numbers, postcodes and reference codes, which you never add up.

What batch size should I use?

The default of 500 rows per INSERT suits most databases and keeps each statement a manageable size. Lower it if your database or client complains about statement length, or set it to 1 to get one INSERT per row, which makes errors easier to locate in a long file.

Are quotes in my data escaped?

Yes. Single quotes inside text values are doubled, so O'Neill becomes 'O''Neill' in the SQL, which every database here accepts. Column names are wrapped in the identifier quotes for your chosen database, so a header such as review count with a space still works.

Where your data goes

SQL is generated by code on this page from the CSV you paste. The data is not sent to a database, a server or Map Extract.

Checked against the tool on

Top