Sub topic 4
Oracle treats the empty string as NULL
Oracle does not distinguish a zero-length string from NULL. Every '' you write is stored and evaluated as NULL. This is one of the best-known differences between Oracle and other databases, and it causes bugs in filters, constraints, string functions, and code ported from elsewhere.
This chapter assumes you have read the earlier chapters in this tutorial. You should know that NULL means "missing or unknown," that comparisons with = yield unknown, and that IS NULL is the correct test. You should also be comfortable with VARCHAR2, DUAL, and basic INSERT and SELECT statements.
The rule
In Oracle SQL, a character value with length zero is NULL. The two spellings below are interchangeable:
SELECT NVL('', 'was null') AS result FROM dual;
SELECT NVL(NULL, 'was null') AS result FROM dual;Both queries return was null. The first one is the point of this chapter. If '' were an ordinary string, NVL would return it unchanged.
Oracle has kept this behavior since its earliest versions. It is documented and stable, and Oracle has said it will not change it, so do not expect a future release to alter it. Oracle documentation also warns that this treatment may not be preserved in the future, but in practice the behavior is relied on by countless applications.
What this means for comparisons
Because '' is NULL, every comparison involving it follows the three-valued logic you already know. The result is unknown, so no row qualifies.
Create a small table to see this. The script is complete and runnable:
CREATE TABLE contact_notes (
note_id NUMBER PRIMARY KEY,
note_text VARCHAR2(100)
);
INSERT INTO contact_notes VALUES (1, 'call back');
INSERT INTO contact_notes VALUES (2, '');
INSERT INTO contact_notes VALUES (3, NULL);
INSERT INTO contact_notes VALUES (4, ' ');
COMMIT;Row 2 holds an empty string, row 3 holds an explicit NULL, and row 4 holds a single space. Oracle stores rows 2 and 3 identically.
Now test three filters:
SELECT note_id FROM contact_notes WHERE note_text IS NULL;
SELECT note_id FROM contact_notes WHERE note_text = '';
SELECT note_id FROM contact_notes WHERE note_text <> '';The results are:
- The first query returns rows 2 and 3.
- The second query returns no rows, because
note_text = NULLis unknown for every row. - The third query also returns no rows, because
note_text <> NULLis unknown too. This surprises people who expect it to return rows 1 and 4.
To find the rows that contain actual text, write:
SELECT note_id FROM contact_notes WHERE note_text IS NOT NULL;This returns rows 1 and 4. Row 4 is included because a single space is a real character, so it is not NULL.
A space is not an empty string
The only string Oracle treats as NULL is the one with zero characters. A string containing a space, a tab, or a newline has length greater than zero and is not NULL:
SELECT LENGTH('') AS len_empty,
LENGTH(' ') AS len_space
FROM dual;len_empty is NULL, not 0, and len_space is 1. The first result is easy to miss. LENGTH of a NULL is NULL, so LENGTH('') = 0 is never true. If you want "empty or missing," test IS NULL.
If a form field might contain only spaces and you want to treat that as missing, trim it first. TRIM removes the spaces, and the zero-length result becomes NULL:
SELECT note_id,
NVL(TRIM(note_text), '(blank)') AS cleaned
FROM contact_notes
ORDER BY note_id;Rows 2, 3, and 4 all return (blank). Row 4 qualifies only because TRIM(' ') produces a zero-length string, which Oracle converts to NULL.
Functions that quietly produce NULL
You do not have to type '' for an empty string to appear. Several string functions return a zero-length result, and Oracle turns that result into NULL:
| Expression | Result | Why |
|---|---|---|
| `TRIM(' ')` | NULL | Trimming leaves zero characters |
| `SUBSTR('abc', 2, 0)` | NULL | A length of 0 yields an empty string |
| `REPLACE('abc', 'abc', '')` | NULL | The replacement is NULL, and the result is empty |
| `RPAD('', 5, '*')` | NULL | The input is NULL, so the output is NULL |
| `UPPER('')` | NULL | The input is NULL |
Each of these can silently introduce NULLs into data you believed was text. When you wrap the result in NVL or COALESCE, the fallback value is used, which is usually what you want. When you pass it to a comparison or a NOT NULL column, it can cause surprises.
Concatenation: the exception to NULL propagation
Earlier in this tutorial you saw that arithmetic with NULL gives NULL. Concatenation is different. The || operator treats NULL as if it were a zero-length string, so it returns the other operand:
SELECT 'abc' || '' || 'def' AS joined_empty,
'abc' || NULL || 'def' AS joined_null
FROM dual;Both columns return abcdef. This is the one place where the empty-string-is-NULL rule works in your favor, because a missing middle name does not erase the rest of a full name.
SELECT first_name || ' ' || middle_name || ' ' || last_name AS full_name
FROM (SELECT 'Ada' AS first_name, '' AS middle_name, 'Lovelace' AS last_name
FROM dual);The result is Ada Lovelace, with two spaces, because the separators still appear. To control spacing, build each optional part with NVL2, COALESCE, or NVL as appropriate.
Note that CONCAT(a, b) behaves the same way, and that mixing NULL into concatenation does not make the whole result NULL. The result is NULL only when every operand is NULL.
Constraints and inserts
Since '' is NULL, inserting an empty string into a NOT NULL column fails:
CREATE TABLE customers (
customer_id NUMBER PRIMARY KEY,
email VARCHAR2(100) NOT NULL
);
INSERT INTO customers VALUES (1, '');Oracle raises ORA-01400: cannot insert NULL into ("YOUR_SCHEMA"."CUSTOMERS"."EMAIL"). The error mentions NULL even though your statement contained a string literal, which can be confusing when the empty value comes from an application variable.
The same logic applies to other constraints:
- A
CHECK (email <> '')constraint never rejects anything, because the condition is unknown for every row, and Oracle accepts rows whose check condition is unknown. UseCHECK (email IS NOT NULL)or aNOT NULLconstraint instead. - A unique constraint allows many rows with an empty string, because it allows many NULLs.
Application code and bind variables
The conversion also happens when an application sends values to the database. When a client program binds an empty string to a VARCHAR2 parameter, the database receives NULL. This applies to JDBC, Python drivers, and most other clients, though the exact API may differ. A form field the user left blank therefore arrives as NULL, and a query that filters with col = :value returns no rows.
In PL/SQL the same rule holds for variables:
SET SERVEROUTPUT ON
DECLARE
v_name VARCHAR2(20) := '';
BEGIN
IF v_name IS NULL THEN
DBMS_OUTPUT.PUT_LINE('v_name is NULL');
ELSE
DBMS_OUTPUT.PUT_LINE('v_name is not NULL');
END IF;
END;
/The block prints v_name is NULL.
Where empty strings come from
Differences from other databases
PostgreSQL, MySQL, SQL Server, and the SQL standard all treat '' as a real, non-NULL string of length zero. In those systems WHERE col = '' is a valid way to find blank values, and LENGTH('') returns 0.
This matters in three situations:
- Porting queries. A filter such as
WHERE name <> ''returns rows elsewhere and nothing in Oracle. - Migrating data. Rows that held
''in the source and NULL in other rows become indistinguishable after loading into Oracle. You cannot recover the original distinction. - Writing portable SQL. Test with
IS NULLfor missing values, and decide explicitly how your application should treat empty input before it reaches the database.
Practical guidance
- Use
IS NULLandIS NOT NULLfor anything that might be blank. Never compare to''. - Normalize user input before testing it.
NVL(TRIM(col), 'default')handles NULL, empty, and whitespace-only values in one expression. - Do not rely on
LENGTH(col) = 0. It returns NULL for empty strings, so no row matches. - If you must keep "empty" distinct from "unknown" in your design, store an explicit marker such as a flag column or a sentinel value like
'N/A'. Oracle cannot store the difference in a plain string column. - When you rewrite code from another database, search for every
= ''and<> ''and review it.
Exercise
A table named orders has a VARCHAR2 column called notes. Some rows hold NULL, some hold a single space, and some hold text. A developer wrote SELECT COUNT(*) FROM orders WHERE notes = ''; hoping to count rows with no notes, but it returns 0 every time. Explain why, and write a query that counts the rows where notes is missing or contains only spaces.
Show answer
Oracle treats '' as NULL, so notes = '' becomes notes = NULL, which is unknown for every row and matches nothing. The correct query is SELECT COUNT(*) FROM orders WHERE TRIM(notes) IS NULL; TRIM turns a space-only value into a zero-length string, which Oracle treats as NULL, and NULL values already satisfy IS NULL.
Exercise
What does SELECT LENGTH('') AS a, LENGTH(' ') AS b FROM dual; return?
Show answer
Column a is NULL, because '' is NULL and LENGTH of NULL is NULL. Column b is 1, because a single space is a real character.