Sub topic 7
DECODE Treats Two NULLs as Equal
This chapter assumes you can write basic SELECT statements, that you know NULL means "unknown or missing value", and that you have seen a simple CASE and a basic DECODE call. Here you will learn the one comparison rule that makes DECODE behave differently from CASE, and how to use it safely.
The rule in one sentence
In SQL, comparing NULL to anything with = gives UNKNOWN, never TRUE. That includes NULL = NULL. DECODE is an exception. When DECODE compares its first argument to a search value, it treats two NULLs as equal.
The Oracle documentation states this directly: in a DECODE function, Oracle considers two nulls to be equivalent. The rule applies only to the comparison between the expression and each search value.
DECODE compared with CASE on the same data
The following query uses the commission_pct column of the employees table. That column is NULL for employees who do not earn a commission.
SELECT employee_id,
commission_pct,
DECODE(commission_pct, NULL, 'No commission', 'Has commission') AS decode_result,
CASE
WHEN commission_pct IS NULL THEN 'No commission'
ELSE 'Has commission'
END AS case_result
FROM employees;Both columns give the same answer for every row. The DECODE version works because, for a row where commission_pct is NULL, the expression and the search value NULL are considered equal. The CASE version needs an explicit IS NULL test, because a comparison with = cannot match NULL.
How DECODE evaluates its arguments
DECODE has this shape:
DECODE(expr, search1, result1, search2, result2, ..., default)Oracle evaluates expr once. It then compares expr with each search value from left to right. When a comparison succeeds, DECODE returns the matching result and stops. If nothing matches, it returns default. If you leave out default, it returns NULL.
The NULL rule changes what "a comparison succeeds" means:
| `expr` value | Search value | Match in DECODE? |
|---|---|---|
| 10 | 10 | Yes |
| 10 | 20 | No |
| NULL | NULL | Yes |
| NULL | 10 | No |
| 10 | NULL | No |
Only the NULL and NULL pairing differs from ordinary SQL comparison.
Why simple CASE does not behave the same way
A simple CASE compares its input expression to each WHEN value using ordinary equality. If either side is NULL, the result is UNKNOWN, and a branch that is not TRUE is skipped. A WHEN NULL branch therefore never matches.
Consider this rewrite of the example above as a simple CASE:
SELECT employee_id,
commission_pct,
CASE commission_pct
WHEN NULL THEN 'No commission'
ELSE 'Has commission'
END AS wrong_result
FROM employees;The statement runs without an error, but it labels every employee as 'Has commission', including those whose commission_pct is NULL. The WHEN NULL branch is never taken, so every row falls to the ELSE branch. This is the most common conversion mistake when people translate DECODE into CASE.
The correct translation uses a searched CASE with IS NULL, as shown earlier.
A decision flow for NULL input
The diagram shows what happens inside each function when the input value is NULL.
Where the NULL rule is useful
The rule lets you write a compact mapping that covers NULL and non-NULL values in one call. For example, you can show a readable label for a missing manager:
SELECT employee_id,
DECODE(manager_id,
NULL, 'Top of hierarchy',
100, 'Reports to the president',
'Reports to someone else') AS reporting_label
FROM employees;Here the NULL search value catches employees with no manager, and the later pairs handle specific values. The same logic in CASE would need a searched form:
SELECT employee_id,
CASE
WHEN manager_id IS NULL THEN 'Top of hierarchy'
WHEN manager_id = 100 THEN 'Reports to the president'
ELSE 'Reports to someone else'
END AS reporting_label
FROM employees;Both queries return the same result. The CASE form is longer, but its intent is explicit.
Order still matters
DECODE checks search values from left to right and stops at the first match. NULL is just one of the possible search values, so its position follows the same rule. If a NULL search value appears before another pair that could also match, only the first match counts. In practice, a NULL input can only match a NULL search value, so overlaps are rare. The more useful point is that every pair is tested in order, so you should put the most specific or most frequent matches first.
Edge cases to know
The default is not a search value
The last argument is treated as the default only when the number of arguments after expr is odd. If the count is even, there is no default. This matters for NULL handling:
SELECT employee_id,
DECODE(commission_pct, NULL, 'No commission') AS no_default
FROM employees;Here the arguments after commission_pct are NULL and 'No commission', which is a complete pair. There is no default, so employees with a commission get NULL in no_default.
NULL as a result is allowed
A result value can also be NULL. For example, DECODE(status, 'X', NULL, status) returns NULL when status is 'X', and returns status otherwise. This is a common way to blank out one specific value.
The first result sets the data type
DECODE uses the data type of the first result value (the first result argument) as the type of the whole expression, and it converts the other results to that type where it can. A literal NULL as the first result is treated as VARCHAR2. Mixing character and numeric results can therefore raise a conversion error or give an unexpected type. Put a typed value first, or use matching types throughout.
The expression is compared after conversion
If expr and a search value have different data types, Oracle converts the search value to the type of expr. Comparing a number column to the string '10' works through implicit conversion, but relying on it hides errors. Use values of the same type as the expression.
Common mistakes
- Translating
DECODE(x, NULL, ...)toCASE x WHEN NULL .... The CASE branch never matches. UseCASE WHEN x IS NULL THEN .... - Assuming
=in a WHERE clause behaves like DECODE.WHERE commission_pct = NULLreturns no rows. UseIS NULL. - Expecting DECODE to treat NULL as a wildcard. NULL only matches NULL. A NULL expression does not match a non-NULL search value, and the reverse also fails.
- Forgetting that a missing default returns NULL. When no pair matches and there is no default, the result is NULL, which can look like a NULL match.
A shortcut for a related case
If the only NULL logic you need is "use this value, or a replacement when it is NULL", DECODE is more than you need. NVL(commission_pct, 0) or COALESCE express that intent directly, and a later chapter covers them. Use DECODE's NULL matching when it sits inside a larger value mapping, as in the manager example.
Exercise
A colleague rewrites DECODE(commission_pct, NULL, 'None', 'Some') as CASE commission_pct WHEN NULL THEN 'None' ELSE 'Some' END. What does the CASE version return for an employee whose commission_pct is NULL, and how should it be written to match the DECODE call?
Show answer
The CASE version returns 'Some' for that employee, because the comparison NULL = NULL is UNKNOWN, so the WHEN NULL branch is skipped and the ELSE branch runs. To match DECODE, use a searched CASE: CASE WHEN commission_pct IS NULL THEN 'None' ELSE 'Some' END.
Exercise
What does DECODE(manager_id, 100, 'A', NULL, 'B') return for a row where manager_id is NULL, and what does it return for a row where manager_id is 205?
Show answer
For the NULL row it returns 'B', because DECODE treats the NULL expression and the NULL search value as equal. For the row with 205 it returns NULL, because no search value matches and the argument count leaves no default.