Full answer
An empty string '' is a real value. It has a length of zero and it equals itself. NULL is the absence of a value:
CREATE TABLE feedback (id INTEGER PRIMARY KEY, note TEXT);
INSERT INTO feedback (note) VALUES ('Great tea'), (''), (NULL), (NULL);SELECT '' IS NULL AS empty_is_null, LENGTH('') AS empty_len, LENGTH(NULL) AS null_len;| empty_is_null | empty_len | null_len |
|---|---|---|
| 0 | 0 | NULL |
They behave differently almost everywhere: WHERE note = '' does not find NULLs, COUNT(note) counts empty strings but not NULLs, and a unique constraint rejects a second '' but allows many NULLs.
SELECT COUNT(*) AS rows_total,
COUNT(note) AS non_null_notes,
SUM(CASE WHEN note = '' THEN 1 ELSE 0 END) AS empty_notes,
SUM(CASE WHEN note IS NULL THEN 1 ELSE 0 END) AS null_notes
FROM feedback;| rows_total | non_null_notes | empty_notes | null_notes |
|---|---|---|---|
| 4 | 2 | 1 | 2 |
Oracle is the exception. It treats an empty string in a VARCHAR2 as NULL, so '' IS NULL is true there. That comes from Oracle documentation and was not run here. Code moved from Oracle to PostgreSQL often breaks on exactly this point.
Having both in one column is a data-quality problem. Searches for "missing note" have to check two things, and reports count them differently. Decide on one meaning. Usually NULL means "not provided", and empty input is converted on the way in with NULLIF(TRIM(x), ''). A CHECK constraint such as CHECK (note <> '') stops empty strings coming back.
CREATE TABLE feedback (id INTEGER PRIMARY KEY, note TEXT);
INSERT INTO feedback (note) VALUES ('Great tea'), (''), (NULL), (NULL);
SELECT '' IS NULL AS empty_is_null, LENGTH('') AS empty_len, LENGTH(NULL) AS null_len;
SELECT COUNT(*) AS rows_total,
COUNT(note) AS non_null_notes,
SUM(CASE WHEN note = '' THEN 1 ELSE 0 END) AS empty_notes,
SUM(CASE WHEN note IS NULL THEN 1 ELSE 0 END) AS null_notes
FROM feedback;Checked on SQLite 3.46.1, run twice with identical output.
Key points
- '' is a value of length zero; NULL is no value
- COUNT, =, and UNIQUE treat them differently
- Oracle stores '' in VARCHAR2 as NULL
- Normalise to one representation and enforce it
Common mistakes
- Searching for missing values with = '' and missing NULL rows
- Assuming Oracle behaviour on PostgreSQL or the reverse
Follow-ups
- How would you clean a column that mixes '', spaces and NULL?
- Should a form field left blank be stored as '' or NULL?
Tips
- Mention Oracle; it is the reason this question is asked