Importing a CSV into a database: how column types, NULLs and quotes are worked out

How each CSV column gets a SQL type, why empty cells become NULL, and how text and names are quoted for SQLite, PostgreSQL, MySQL and SQL Server.

Open the CSV to SQL Free, in your browser, nothing uploaded.

A CSV file is just text: rows on lines, cells split by commas. A database table is stricter: every column has a type, and every value must fit it. Getting from one to the other means making three decisions for every column: what type it is, what an empty cell means, and how text is quoted so it cannot break the SQL. This guide explains how those decisions are made, so you can trust the result or spot when to change it. It is for analysts, students and small-business owners moving spreadsheets into SQLite, PostgreSQL, MySQL or SQL Server. Before you convert, look over the data with CSV profiler; afterwards, test the SQL in SQL playground and tidy any queries with SQL formatter.

What it is

Reading the CSV. A cell wrapped in double quotes may hold commas, line breaks and doubled quotes ("" means one "). So "Lee, Min" is one cell, not two. The separator is guessed by counting commas, tabs, semicolons and bars in the first five lines.

Guessing types. Every non-empty value in a column is tested, and the first rule that all of them pass wins:

  • all true or false → boolean;
  • all whole numbers → integer, or a big integer beyond ±2,147,483,647;
  • all numbers with a decimal point or exponent (whole numbers allowed too) → floating point;
  • all real calendar dates like 2026-12-01 → date; with a time → timestamp;
  • anything else → text.

A number with a leading zero, such as a postcode 00123, fails the integer rule on purpose and stays text, so the zero survives.

Names and types per database. Column names are cleaned (spaces and symbols become _) and quoted the way each database expects: "name" in SQLite and PostgreSQL, `name` in MySQL, [name] in SQL Server. Types are translated too: a boolean is BOOLEAN in PostgreSQL and MySQL, BIT in SQL Server and INTEGER (1 or 0) in SQLite, which has no separate boolean.

Empty cells and NULL. NULL means “no value”, which is different from an empty piece of text. An empty number, date or true/false cell can only mean NULL, because '' is not a valid number or date. For text columns you choose: NULL, or an empty string.

Escaping text. An apostrophe inside text is doubled: O'Brien becomes 'O''Brien'. MySQL also needs backslashes doubled. SQL Server marks text with non-English letters as N'...' so it is stored as Unicode.

How to use it

  1. Open the CSV to SQL page and paste the data, or choose a file.
  2. Pick the database, type a table name and say whether the first row is a header.
  3. Check the separator and the Columns found table; a surprising type usually means a stray value.
  4. Set rows per INSERT, and tick or untick CREATE TABLE, DROP TABLE IF EXISTS, empty cells as NULL and lower-case names.
  5. Copy the SQL or download the .sql file, then run it in your database.

A worked example

A small business exports three invoices:

order_no,customer,amount,paid,due
1001,Asha Rao,249.50,true,2026-12-01
1002,"Lee, Min",,false,2026-12-15
1003,O'Brien & Sons,1200,TRUE,

For PostgreSQL with table name “Invoices Q4”, the table becomes "Invoices_Q4" and the columns are typed:

  • order_no INTEGER (all whole numbers);
  • customer TEXT (the quotes around Lee, Min keep it one cell);
  • amount DOUBLE PRECISION (249.50 has a decimal point, 1200 is allowed alongside it);
  • paid BOOLEAN (TRUE and true both count);
  • due DATE.

All three rows go into one INSERT of 362 characters including the CREATE TABLE. Row 2’s missing amount is written as NULL, row 3’s missing due date as NULL, and the third customer as 'O''Brien & Sons'. For MySQL the same file gives INT, VARCHAR(20), DOUBLE, BOOLEAN and DATE, with true and false written as 1 and 0.

Checked with the tools

The example was run through the tool’s own csvToSql() (src/lib/dev/csvsql.ts) in Node for all four databases, and the types, the 362-character length and the escaped values above are copied from its output. While checking, we found a bug: with “Empty cells become NULL” unticked, an empty number cell was written as nothing at all ('Lee, Min', , FALSE), which no database accepts, and an empty date became ''. Empty number, date and true/false cells now always become NULL, and only text keeps an empty string. A test covers it.

Where it is used

  • Analysis. Load an export into SQLite to join, group and filter it with SQL instead of formulas.
  • Small apps. Seed a new database with a product list or price table.
  • Teaching. Turn any open data CSV into a practice table for students.
  • Migrations. Move a sheet that has outgrown a spreadsheet into a shared database.

Common mistakes

  • Money as floating point. Floating types can round in the last digits. Change amounts to DECIMAL(12,2) before running.
  • Codes stored as numbers. Phone numbers and IDs with leading zeros must stay text.
  • Dates in local formats. 01/12/2026 is ambiguous and stays text. Convert dates to YYYY-MM-DD first.
  • DROP TABLE on live data. It deletes the existing table. Use it only for test tables you rebuild.

Questions people ask

Why did a number column become text? At least one value is not a plain number, such as “n/a”, “1,200” or a currency sign.

How many rows per INSERT? A few hundred to a thousand loads quickly; SQL Server allows at most 1,000.

Why is my date column TEXT in SQLite? SQLite has no date type; ISO text dates work with its date functions.

Sources

  • fibub's own CSV reader (quoted cells, doubled quotes, line breaks inside quotes, byte-order mark) and writer. Type names follow each database's documentation: SQLite type affinity, PostgreSQL data types, MySQL and SQL Server data types (checked 2026-12-05). SQL Server allows up to 1,000 rows in one VALUES list. The guessed types are a starting point, so check them before running the SQL.
  • CSV quoting follows RFC 4180: a cell with a comma, quote or line break is wrapped in double quotes, and a quote inside it is doubled.

If you are an author or publisher and want a credit or a passage changed or removed, tell us and we will act quickly.

Tools in this article

Read next