Data & Numbers

Getting a spreadsheet into a database without an import wizard

Every database has a bulk import command, and each one is slightly different. For a small or medium table, a plain SQL script is often faster to get right.

3 minute read

A spreadsheet arrives, and the data in it needs to be in a database: a product list for a new shop, survey responses for analysis, seed data for a test environment. Every database has a bulk loader, such as COPY in PostgreSQL, LOAD DATA INFILE in MySQL and .import in SQLite, and each has its own syntax, permissions and quirks. For a table of a few thousand rows, there is often a simpler route.

When a SQL script is the right tool

An INSERT script is plain SQL: a CREATE TABLE statement followed by INSERT statements with the data written out. That has real advantages:

  • It runs anywhere you can paste SQL: a GUI client, a web console, a migration file, a terminal.
  • You can read it before running it, and see exactly what types and values are about to go in.
  • It needs no file access on the server, which bulk loaders often do and managed databases often forbid.
  • It can live in version control as seed data, next to the code that uses it.

For millions of rows, the native bulk loader is much faster and worth learning. For everything smaller, a script is usually quicker to get right.

How the types get chosen

The CSV to SQL converter reads a CSV, TSV or Excel file and picks a type for each column by looking at every value in it. A column becomes BOOLEAN if every value is true or false, INTEGER if every value is a whole number, REAL if every value is numeric, DATE if every value is written YYYY-MM-DD, and TEXT otherwise. Empty cells are ignored when choosing.

Two rules are deliberately conservative. A value with a leading zero, such as 00745, keeps its whole column as TEXT, because storing it as a number would delete the zero. And numbers longer than 18 digits stay text, because they would overflow or lose precision. Order numbers, postcodes, phone numbers and account IDs are identifiers, not quantities, and TEXT is the right type for them even when they look numeric.

Column names are cleaned for SQL: lowercased, spaces turned into underscores, symbols removed, and duplicates numbered. Each sheet of a workbook becomes its own table.

Three things to fix before you run it

Empty cells. The converter writes an empty cell as NULL. That is usually right, but it matters: WHERE phone = '' will not find a NULL, and COUNT(phone) skips them. Decide what "no value" means for each column now, because changing it later means updating every row.

Dates in local formats. Only ISO dates are recognised as DATE. A column of 17/10/2026 stays text, because it could be 17 October or, in another locale, nonsense. Convert the column to YYYY-MM-DD in the spreadsheet first, or use Excel to CSV, which writes Excel's real date cells as ISO dates.

Duplicates and stray whitespace. A trailing space makes "Mumbai " and "Mumbai" two different cities, and a duplicated row becomes a duplicated record. Run the file through a CSV cleaner and remove duplicates before converting.

Running it well

Rows are written in batches of 500 per INSERT, which keeps each statement under the default size limits of every common server. Two habits make the rest go smoothly:

  1. Wrap it in a transaction. Add BEGIN; at the top and COMMIT; at the end. Thousands of separate commits become one, which is dramatically faster, and if a row fails, nothing half-loads.
  2. Add keys and indexes afterwards. The script creates a plain table with no primary key or constraints. Load the data, check it, then add the primary key, foreign keys and indexes you need. Building an index once after the load is faster than updating it on every insert.

One portability note: SQL Server has no BOOLEAN type or TRUE/FALSE literals. If you are loading into SQL Server, change BOOLEAN columns to BIT and the values to 1 and 0 before running the script.

Further reading

Do it hereCSV to SQLTurn CSV or Excel data into CREATE TABLE and INSERT statements for MySQL, PostgreSQL or SQLiteOpen

Written for NextTool. Every statement about a tool is checked against that tool's code, and corrections are dated above. How guides are written

sqlcsvexceldatabaseimport