Sub topic 13
Related Functions: NULLIF and DECODE
Assume you know basic SELECT statements, Oracle data types, DUAL, and aggregate functions. NULLIF and DECODE are older Oracle functions that solve specific conditional problems involving NULL. They are related to COALESCE, but they do different jobs.
Using NULLIF to turn a value into NULL
NULLIF compares two expressions:
NULLIF(expression1, expression2)It returns NULL when the two expressions are equal. Otherwise, it returns expression1.
This is equivalent to the following logic:
CASE
WHEN expression1 = expression2 THEN NULL
ELSE expression1
ENDFor example, suppose a report stores a zero commission as a value that should not be displayed:
SELECT employee_id,
NULLIF(commission_pct, 0) AS meaningful_commission
FROM employees;A commission of zero becomes NULL. A nonzero commission remains unchanged. An existing NULL also remains NULL.
A common use is preventing division by zero. The expression sales / 0 raises an error. NULLIF changes the zero denominator to NULL:
SELECT product_id,
sales,
returns,
sales / NULLIF(returns, 0) AS sales_per_return
FROM product_metrics;When returns is zero, the denominator becomes NULL, so the result is NULL instead of an ORA-01476 error. This is useful when “not calculable” is a better result than stopping the query.
Using DECODE for Oracle-style branching
DECODE compares one expression with several search values:
DECODE(expression,
search_value_1, result_1,
search_value_2, result_2,
default_result)It returns the result associated with the first matching search value. If nothing matches, it returns the default result. Without a default result, it returns NULL.
For example:
SELECT employee_id,
department_id,
DECODE(department_id,
10, 'Administration',
20, 'Marketing',
30, 'Purchasing',
'Other') AS department_name
FROM employees;DECODE is similar to a simple CASE expression:
SELECT employee_id,
CASE department_id
WHEN 10 THEN 'Administration'
WHEN 20 THEN 'Marketing'
WHEN 30 THEN 'Purchasing'
ELSE 'Other'
END AS department_name
FROM employees;CASE is usually easier to read, especially when conditions are more complex. DECODE remains important in older Oracle code and has one notable behavior: it treats two NULL values as equal.
SELECT DECODE(NULL,
NULL, 'matched',
'not matched') AS result
FROM dual;This returns matched. A normal comparison such as NULL = NULL does not evaluate to TRUE. That special DECODE behavior can be useful, but it can also surprise readers.
Choosing among the functions
| Need | Suitable function | Example |
|---|---|---|
| Replace one `NULL` with a fallback | `NVL` | `NVL(phone, 'unknown')` |
| Return the first available value | `COALESCE` | `COALESCE(mobile, work, home)` |
| Turn a matching value into `NULL` | `NULLIF` | `NULLIF(status, 'N/A')` |
| Map several exact values to labels | `DECODE` or `CASE` | `DECODE(code, 1, 'Open', 'Other')` |
The functions fit together in larger expressions. For example, NULLIF can remove a placeholder, and COALESCE can then choose a fallback:
SELECT COALESCE(NULLIF(preferred_phone, 'N/A'),
work_phone,
'no phone on file') AS contact_phone
FROM contacts;First, NULLIF converts N/A to NULL. Then COALESCE returns the first remaining non-NULL phone value.
Exercise
Which expression returns NULL when denominator is zero, but otherwise divides numerator by denominator?
Show answer
numerator / NULLIF(denominator, 0)