Loading a CSV file into a relational database should be a five-minute job. In practice, it regularly fails halfway through: a customer named O'Brien breaks a string literal, a Windows file path with a backslash disappears in MySQL, a New England ZIP code (02134) loses its leading zero in an integer column, or an empty cell is stored as an empty string "" instead of NULL.

Even worse, a SQL script that runs cleanly in PostgreSQL often fails or silently alters data when run in MySQL, SQLite or Microsoft SQL Server. Each database has its own rules for quoting table and column names, escaping characters inside strings, handling Unicode text, and batching INSERT rows.

This guide walks through one small, realistic CSV dataset, shows the exact problems that break an import, compares the four major SQL dialects side by side, and explains when to use INSERT statements versus native bulk loaders.

Four cards comparing PostgreSQL, MySQL and MariaDB, SQLite, and Microsoft SQL Server: identifier quotes, apostrophe and backslash escaping, safe ID column types, and bulk loader commands.
The four databases agree on basic SQL, but differ on identifier quotes, backslashes, Unicode literals and batch sizes.

One small CSV that trips up naive imports

Suppose you export three customer records from a CRM or spreadsheet:

customer_id,full_name,city,postal_code,export_path,lifetime_value
00142,"O'Brien, Pat",Zürich,02134,C:\exports\north,1250.50
00143,Morgan Lee,Leeds,LS1 4AP,,0
00144,Tomás Ortega,Madrid,28013,C:\exports\south,89.00

Look closely at what is inside those three rows:

  • 00142 and 02134 look like integers, but they have leading zeros that are part of the identifier.
  • "O'Brien, Pat" contains both a comma (quoted in CSV) and an apostrophe (which closes a SQL string literal if unescaped).
  • Zürich and Tomás contain non-ASCII characters.
  • C:\exports\north contains backslashes (\e and \n).
  • Row 2 has an empty field (,,) for export_path.
  • 1250.50 is a decimal currency amount, not a floating-point approximation.

Every one of those six details corresponds to a classic CSV-to-SQL failure mode.

The six things that break a CSV-to-SQL import

1. Apostrophes inside values (O'Brien)

In SQL, string literals are wrapped in single quotes: 'Leeds'. When a value itself contains a single quote — a surname like O'Brien or a note like Don't ship before Friday — a naive script that wraps values in '…' produces:

-- Broken: syntax error at "Brien"
INSERT INTO customers (full_name) VALUES ('O'Brien, Pat');

Standard SQL escapes a single quote by doubling it (''), not with a backslash:

-- Valid in PostgreSQL, MySQL, SQLite and SQL Server
INSERT INTO customers (full_name) VALUES ('O''Brien, Pat');

2. Backslashes in MySQL (C:\exports\north)

In PostgreSQL (since version 9.1, when standard_conforming_strings became the default), SQLite and SQL Server, a backslash inside '…' is an ordinary character. In MySQL and MariaDB (unless the server runs with NO_BACKSLASH_ESCAPES), a backslash starts an escape sequence: \n becomes a line break, \t becomes a tab, \0 becomes a NUL byte, and \' escapes a single quote.

If you insert 'C:\exports\north' into MySQL without escaping backslashes, the \n in \north is stored as a literal newline character. Even worse, if a CSV field ends with a trailing backslash (C:\exports\), that backslash escapes the closing ', causing MySQL to treat the comma and the next column as part of the string.

The fix: when generating SQL for MySQL or MariaDB, double every backslash (\\) and double every single quote (''):

-- MySQL / MariaDB
INSERT INTO `customers` (`export_path`) VALUES ('C:\\exports\\north');

3. Unicode text in SQL Server (N'Zürich')

PostgreSQL, MySQL (when the table uses utf8mb4) and SQLite accept UTF-8 characters directly inside standard single-quoted strings ('Zürich').

Microsoft SQL Server is different. A plain string literal 'Zürich' is treated as a VARCHAR literal and converted to the database’s default 8-bit code page before it is stored — even if the target column is NVARCHAR. Any character outside that code page (accented letters from other scripts, Greek or Cyrillic letters, Japanese kanji, or emoji) is silently replaced with ?. To keep Unicode intact in SQL Server, prefix string literals with N:

-- SQL Server: N prefix marks a Unicode (NVARCHAR) literal
INSERT INTO [customers] ([city]) VALUES (N'Zürich');

4. Empty cells vs NULL

In a CSV file, two commas with nothing between them (LS1 4AP,,0) represent an empty cell. Because CSV is plain text, it has no native keyword to distinguish NULL (no value recorded) from '' (a zero-length string).

When converting CSV to SQL, writing empty cells as '' causes two problems:

  1. Numeric and date columns reject ''. In PostgreSQL and strict-mode MySQL, INSERT INTO orders (amount, shipped_at) VALUES ('', '') fails immediately because an empty string is not a valid number or date. NULL, however, is accepted by any nullable column of any type.
  2. Queries behave differently. COUNT(export_path) and WHERE export_path IS NULL only work as expected when missing values are stored as NULL.

The fix: convert empty CSV cells to unquoted NULL by default, and provide an option to keep them as '' when a table uses NOT NULL DEFAULT '' text columns.

5. Over-eager numeric column types and FLOAT rounding

Many import wizards inspect the first 10 or 50 rows of a CSV and guess column types. That causes three common bugs:

  • Leading zeros disappear. 00142 and 02134 look like integers. Stored in an INTEGER or BIGINT column, they become 142 and 2134. The leading zeros are permanently lost.
  • Later rows fail the import. If rows 1–100 have US 5-digit ZIP codes (02134) and row 101 has a UK postcode (LS1 4AP) or a ZIP+4 (02134-1305), an INTEGER column aborts the import halfway through.
  • Prices get rounded in FLOAT or REAL. Binary floating-point types cannot represent 0.10 or 1250.50 exact in all arithmetic. Currency and exact decimal measurements belong in NUMERIC(p, s) / DECIMAL(p, s) — or TEXT if you plan to cast them inside SQL.

The fix: default every column to TEXT (NVARCHAR(MAX) in SQL Server), which guarantees the import succeeds without changing a single character. Only promote a column to BIGINT or DECIMAL after scanning every non-empty row in the file and confirming that every value is a plain number with no leading zeros, no scientific notation, and no more than 15–18 digits.

6. Weird column headers and reserved words

CSV headers are written by humans and spreadsheet apps: Order ID, user-email, group, select, total ($), or even a duplicate email column. Unquoted in SQL, spaces and hyphens are syntax errors, and words like group, order, user or select clash with SQL reserved keywords.

Additionally, if a file was saved as “CSV UTF-8” by Excel, the first header starts with an invisible 3-byte UTF-8 byte order mark (\uFEFFcustomer_id), so queries for SELECT customer_id fail with “column does not exist”.

The fix: strip any UTF-8 BOM before reading the header row, rename blank or duplicate headers (email, email_2), and always wrap table and column names in the database’s identifier quotes.

Side-by-side: the same CSV in all four SQL dialects

Here is how our sample CSV looks when converted with safe numeric detection enabled across each database.

PostgreSQL:

CREATE TABLE "customers" (
  "customer_id" TEXT,
  "full_name" TEXT,
  "city" TEXT,
  "postal_code" TEXT,
  "export_path" TEXT,
  "lifetime_value" NUMERIC(6, 2)
);

INSERT INTO "customers" ("customer_id", "full_name", "city", "postal_code", "export_path", "lifetime_value") VALUES
  ('00142', 'O''Brien, Pat', 'Zürich', '02134', 'C:\exports\north', 1250.50),
  ('00143', 'Morgan Lee', 'Leeds', 'LS1 4AP', NULL, 0),
  ('00144', 'Tomás Ortega', 'Madrid', '28013', 'C:\exports\south', 89.00);

MySQL / MariaDB (backticks for names, doubled backslashes in strings):

CREATE TABLE `customers` (
  `customer_id` TEXT,
  `full_name` TEXT,
  `city` TEXT,
  `postal_code` TEXT,
  `export_path` TEXT,
  `lifetime_value` DECIMAL(6, 2)
);

INSERT INTO `customers` (`customer_id`, `full_name`, `city`, `postal_code`, `export_path`, `lifetime_value`) VALUES
  ('00142', 'O''Brien, Pat', 'Zürich', '02134', 'C:\\exports\\north', 1250.50),
  ('00143', 'Morgan Lee', 'Leeds', 'LS1 4AP', NULL, 0),
  ('00144', 'Tomás Ortega', 'Madrid', '28013', 'C:\\exports\\south', 89.00);

Microsoft SQL Server (square brackets for names, N'…' Unicode literals):

CREATE TABLE [customers] (
  [customer_id] NVARCHAR(MAX),
  [full_name] NVARCHAR(MAX),
  [city] NVARCHAR(MAX),
  [postal_code] NVARCHAR(MAX),
  [export_path] NVARCHAR(MAX),
  [lifetime_value] DECIMAL(6, 2)
);

INSERT INTO [customers] ([customer_id], [full_name], [city], [postal_code], [export_path], [lifetime_value]) VALUES
  (N'00142', N'O''Brien, Pat', N'Zürich', N'02134', N'C:\exports\north', 1250.50),
  (N'00143', N'Morgan Lee', N'Leeds', N'LS1 4AP', NULL, 0),
  (N'00144', N'Tomás Ortega', N'Madrid', N'28013', N'C:\exports\south', 89.00);

Notice that in all three outputs, customer_id (00142) and postal_code (02134) stay TEXT / NVARCHAR(MAX) because they contain leading zeros or letters, while lifetime_value safely becomes NUMERIC(6, 2) / DECIMAL(6, 2).

Dialect comparison table

Rule PostgreSQL MySQL / MariaDB SQLite SQL Server
Identifier quotes "table_name" `table_name` "table_name" [table_name]
Escaping a quote in identifier "a""b" `ab` `` "a""b" [a]]b]
String literal 'O''Brien\x' 'O''Brien\\x' 'O''Brien\x' N'O''Brien\x'
Safe text type TEXT TEXT TEXT NVARCHAR(MAX)
Exact decimal type NUMERIC(p, s) DECIMAL(p, s) NUMERIC DECIMAL(p, s)
Rows per INSERT … VALUES Multi-row (100–1,000) Multi-row (100–1,000) Multi-row (100–1,000) Max 1,000 rows
Transaction wrapper BEGIN; … COMMIT; START TRANSACTION; … COMMIT; BEGIN TRANSACTION; … COMMIT; BEGIN TRANSACTION; … COMMIT;

Pay special attention to the 1,000-row cap in SQL Server: T-SQL’s table value constructor (INSERT INTO … VALUES (…), (…)) refuses statements with more than 1,000 rows (Msg 10738). Any tool generating SQL for SQL Server must cap each batch at 1,000 rows and start a new INSERT statement on row 1,001.

Why batching and transactions speed up imports by 10–50×

If you run 20,000 separate single-row INSERT statements without a transaction, the database treats each row as an independent transaction and flushes its write-ahead log to disk 20,000 times. In SQLite or a remote cloud database, that can take several minutes.

Doing two things transforms both speed and safety:

  1. Batch 100 to 1,000 rows into each INSERT … VALUES statement. That cuts SQL parsing overhead and network round-trips by 99%.
  2. Wrap the entire script in BEGIN … COMMIT. The database writes to disk once when committing, and if row 14,200 hits a constraint error, the whole import rolls back cleanly rather than leaving 14,199 rows in a half-populated table.

INSERT statements vs native bulk loaders

When should you generate INSERT statements, and when should you use the database’s native CSV loader?

  • Use INSERT statements (up to ~50,000 rows) when you are pasting into a SQL GUI (pgAdmin, DBeaver, TablePlus, MySQL Workbench, SSMS), checking a seed file into Git migrations, or importing over a connection where the database server cannot read files on your laptop.
  • Use a native bulk loader (hundreds of thousands or millions of rows) when raw throughput matters:
    • PostgreSQL (psql): \copy "customers" FROM 'customers.csv' WITH (FORMAT csv, HEADER true, ENCODING 'UTF8');
    • MySQL / MariaDB: LOAD DATA LOCAL INFILE 'customers.csv' INTO TABLE customers CHARACTER SET utf8mb4 FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n' IGNORE 1 LINES;
    • SQLite CLI: .mode csv followed by .import customers.csv customers
    • SQL Server: BULK INSERT [customers] FROM 'C:\path\customers.csv' WITH (FORMAT = 'CSV', FIRSTROW = 2, CODEPAGE = '65001');

A safe 4-step workflow with RapidConvertor

  1. Combine or split files if needed. If your data arrives as separate monthly exports, combine and align their headers first with the CSV Merger. If a CSV is too big for your SQL editor, break it into smaller chunks with the CSV Splitter.
  2. Clean the CSV before generating SQL. Run the file through the CSV Cleaner to strip invisible spaces, remove blank lines, and deduplicate repeated rows.
  3. Generate dialect-specific SQL. Open the CSV to SQL converter, choose PostgreSQL, MySQL, SQLite or SQL Server, inspect the parsed column types and preview table, and toggle “Detect numeric columns” only if you want plain-number columns promoted to BIGINT / DECIMAL.
  4. Wrap in a transaction. Check “Wrap in a transaction” so the database loads every row or none at all. Everything runs locally in your browser — your database rows never leave your device.