Sub topic 13
NVL, NVL2, COALESCE and NULLIF as Shorter Alternatives
Prerequisites
This chapter assumes that you can write basic Oracle SELECT statements and understand that NULL means an unknown or missing value. These functions provide compact alternatives to common CASE expressions.
Replacing one NULL value with another
NVL returns its first argument when that argument is not NULL. Otherwise, it returns the second argument.
SELECT employee_id,
commission_pct,
NVL(commission_pct, 0) AS commission_rate
FROM employees;If commission_pct is NULL, the query displays 0. Otherwise, it displays the stored commission percentage.
The equivalent searched CASE expression is:
CASE
WHEN commission_pct IS NULL THEN 0
ELSE commission_pct
ENDThe two arguments to NVL should have compatible data types. Oracle may implicitly convert one argument to the type of the other, but explicit conversion is safer when the types differ.
Choosing between two results with NVL2
NVL2 tests whether its first argument is NULL. It returns its second argument when the first is not NULL, and its third argument when the first is NULL.
SELECT employee_id,
commission_pct,
NVL2(commission_pct,
'Commission applies',
'No commission') AS commission_status
FROM employees;This is equivalent to:
CASE
WHEN commission_pct IS NOT NULL THEN 'Commission applies'
ELSE 'No commission'
ENDUse NVL2 when you need exactly two outcomes based on the presence of a value. Use CASE when the conditions are more complex or there are more than two outcomes.
Selecting the first available value with COALESCE
COALESCE returns the first non-NULL expression in its argument list. It is useful when several fallback values are possible.
SELECT employee_id,
COALESCE(commission_pct, salary, 0) AS first_available_value
FROM employees;Oracle checks commission_pct first, then salary, and finally 0. COALESCE is equivalent to a searched CASE with IS NOT NULL tests.
Oracle uses short-circuit evaluation for COALESCE, so it does not evaluate later expressions after finding a non-NULL value.
Returning NULL when two values match with NULLIF
NULLIF compares two expressions. It returns NULL when they are equal; otherwise, it returns the first expression.
SELECT employee_id,
salary,
NULLIF(salary, 0) AS salary_for_division
FROM employees;This can prevent a zero value from being used in a later calculation. Its equivalent CASE expression is:
CASE
WHEN salary = 0 THEN NULL
ELSE salary
ENDNULLIF cannot use the literal NULL as its first argument. Also remember that comparisons with NULL use unknown rather than true or false.
Choosing the right function
flowchart TD
A[Need conditional handling] --> B{One value may be NULL?}
B -->|Replace it| C[NVL]
B -->|Choose two results| D[NVL2]
B -->|Use several fallbacks| E[COALESCE]
B -->|Turn equality into NULL| F[NULLIF]
B -->|More complex conditions| G[CASE]Choose CASE when readability, multiple conditions, ranges, or different data types require precise control. Choose the shorter function when its exact rule describes the requirement clearly.
Exercise
Which expression returns Unknown when both preferred_name and legal_name are NULL?
Show answer
COALESCE(preferred_name, legal_name, 'Unknown').