Sub topic 4
Oracle Treats the Empty String as NULL
Assume you know basic SELECT statements, WHERE clauses, aliases, Oracle character types such as VARCHAR2, and the DUAL table. This chapter focuses on a special Oracle rule: in SQL, a zero-length character string written as '' is treated as NULL.
The rule for character values
A NULL means that a value is missing, unknown, or not applicable. An empty string usually means that a value has zero characters. Oracle SQL does not keep these states separate for ordinary character columns.
For example, both inserts store a NULL in note:
CREATE TABLE messages (
message_id NUMBER,
note VARCHAR2(30)
);
INSERT INTO messages (message_id, note) VALUES (1, NULL);
INSERT INTO messages (message_id, note) VALUES (2, '');
SELECT message_id,
note,
CASE
WHEN note IS NULL THEN 'NULL'
ELSE 'non-NULL'
END AS stored_state
FROM messages
ORDER BY message_id;The result identifies both rows as NULL. The literal '' does not represent a stored empty string in this context.
Testing an empty string
Because Oracle converts '' to NULL, use IS NULL to test it. Do not use = ''.
SELECT 'empty string' AS value_description
FROM dual
WHERE '' IS NULL;
SELECT 'matched' AS value_description
FROM dual
WHERE '' = '';The first query returns one row. The second query returns no rows because it is effectively testing NULL = NULL, and that comparison is not true.
The same rule affects NVL:
SELECT NVL('', 'replacement') AS result
FROM dual;Oracle treats the first argument as NULL, so the result is replacement.
Empty strings in expressions
An empty string can appear to behave differently during concatenation. Oracle treats a null character value as having no characters when concatenating it with another character value:
SELECT 'Hello' || '' AS first_result,
'' || 'World' AS second_result,
'Hello' || NULL AS third_result
FROM dual;Each result is the non-null text: Hello, World, and Hello. This behavior is an exception to the general rule that a NULL operand makes an expression NULL.
Arithmetic follows the general rule instead. An empty string becomes NULL, and arithmetic with NULL produces NULL:
SELECT 10 + '' AS result
FROM dual;The result is NULL.
Detecting missing text safely
Use IS NULL when you need rows where a character column contains either an explicitly inserted NULL or an empty string:
SELECT message_id
FROM messages
WHERE note IS NULL;Use NVL when you need display text or a calculation that substitutes a value:
SELECT message_id,
NVL(note, '[no note]') AS displayed_note
FROM messages
ORDER BY message_id;If an application needs to distinguish “not supplied” from “supplied but empty,” an Oracle VARCHAR2 column alone cannot preserve that distinction. Store an additional status value, or use a design that records the distinction separately.
Exercise
What rows does this query return, and why? SELECT message_id FROM messages WHERE note = '';
Show answer
It returns no rows. Oracle treats '' as NULL, so the condition becomes note = NULL. Use WHERE note IS NULL instead.