MySQL load NULL values from CSV data
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Importing CSV data into MySQL becomes tricky as soon as missing values are involved. A CSV file might represent “no value” as an empty field, the literal text NULL, or the MySQL-style marker \N, and those are not the same thing unless you map them deliberately during import.
Decide What the CSV Means
Before writing the LOAD DATA statement, inspect how the source file represents missing values. These are the patterns you will usually see:
- empty fields such as
,, - the text
NULL - the marker
\N
MySQL can treat those differently. An empty field may become an empty string for a text column, while \N is commonly interpreted as a real SQL null in text imports. If the file mixes formats, you need explicit conversion rules.
Import into User Variables First
The safest pattern is to load raw values into user variables and then normalize them in the SET clause.
This pattern gives you control over every column. In the example above, both an empty string and the text NULL are converted into SQL NULL.
Example Table and CSV
Suppose the destination table looks like this:
And the CSV contains this data:
Without cleanup, the import can leave you with a mixture of empty strings and actual nulls in the same logical field. Using variables and NULLIF makes the result predictable.
Preserve Empty Strings When Needed
Do not assume empty string and null are interchangeable. Sometimes a blank string is meaningful, especially in text columns. If you want to preserve empty text but still convert explicit NULL markers, change the mapping.
This distinction matters later because WHERE notes = '' and WHERE notes IS NULL are different queries.
Check the Import Environment
A correct SQL statement can still fail because of the environment around it. A few settings matter a lot:
- '
LOAD DATA INFILEreads from the MySQL server machine.' - '
LOAD DATA LOCAL INFILEreads from the client side.' - '
secure_file_privcan restrict readable directories.' - line endings such as
\r\ncan affect how the last field is parsed.
If null handling looks wrong, inspect the raw file first. Many import bugs come from invisible carriage returns, inconsistent quoting, or a client that does not allow LOCAL.
Common Pitfalls
- Assuming an empty CSV field automatically becomes SQL
NULL. - Forgetting that the string
"NULL"is still text unless you convert it. - Mixing several null conventions in one CSV export.
- Using
LOAD DATA INFILEwhen the file is actually on the client machine.
Summary
- Decide whether missing values are encoded as empty fields,
NULL, or\N. - Use user variables plus
NULLIFwhen null conversion needs to be explicit. - Treat empty string and SQL
NULLas different values unless you intentionally collapse them. - Choose
LOCALor non-LOCALimport based on where the file actually lives. - Check file formatting and server settings when the SQL looks correct but the import still misbehaves.

