Sub topic 10
NVL, NVL2, or COALESCE: choosing the right one
Assume you know basic SELECT statements, Oracle data types, DUAL, and aggregate functions. This chapter focuses on choosing among three functions that handle NULL, Oracle's marker for a missing or unknown value.
The choice at a glance
Use the function that matches the shape of the decision:
| Function | Main purpose | Number of input expressions |
|---|---|---|
| `NVL` | Replace one possible `NULL` value | 2 |
| `NVL2` | Choose between two results | 3 |
| `COALESCE` | Return the first non-`NULL` value | 2 or more |
The following diagram summarizes the decision:
Use NVL for one replacement
NVL(expression, replacement) returns replacement when expression is NULL. Otherwise, it returns expression.
A common use is calculating pay when commission_pct might be missing:
SELECT employee_id,
salary + salary * NVL(commission_pct, 0) AS total_pay
FROM employees;Without NVL, a missing commission makes the multiplication and addition produce NULL. With NVL, the missing commission acts as zero.
Choose NVL when the rule is simple: “Use this value, or use this one default.”
Use NVL2 for two branches
NVL2(expression, value_if_not_null, value_if_null) returns one result when the first expression is not NULL and another result when it is NULL.
SELECT employee_id,
NVL2(
commission_pct,
'Salary plus commission',
'Salary only'
) AS pay_type
FROM employees;Here, the function does not return the commission itself. It classifies each employee into one of two labels.
Use NVL2 when you need a compact two-way choice based on whether a value exists. If you need several conditions unrelated to NULL, use CASE instead.
Use COALESCE for several fallbacks
COALESCE(expression1, expression2, ...) returns the first expression that is not NULL.
SELECT contact_id,
COALESCE(
mobile_phone,
work_phone,
home_phone,
'no phone on file'
) AS best_phone
FROM contacts;This reads as: use the mobile number; if it is missing, use the work number; then the home number; otherwise use the text message.
COALESCE can also replace a two-argument NVL:
SELECT COALESCE(commission_pct, 0) AS commission_pct
FROM employees;For two arguments, NVL may fit existing Oracle code. For multiple fallbacks, COALESCE expresses the intent more clearly and follows the SQL standard.
Data types and evaluation behavior
The replacement expressions must have compatible data types. For example, this expression can fail because Oracle cannot convert the text 'none' into a number:
SELECT NVL(commission_pct, 'none')
FROM employees;Convert the numeric column to text when the desired result is text:
SELECT NVL(TO_CHAR(commission_pct), 'none') AS commission
FROM employees;NVL and COALESCE also differ in evaluation rules. COALESCE uses short-circuit evaluation: Oracle stops at the first non-NULL expression. Do not rely on NVL to protect a risky fallback expression from evaluation. If evaluation order matters, COALESCE or an explicit CASE makes the intent safer.
A practical selection rule
- Choose
NVLfor one expression and one default. - Choose
NVL2for two result branches based onNULLstatus. - Choose
COALESCEfor an ordered list of fallbacks or when short-circuit behavior matters. - Check the resulting data type before mixing numbers, dates, and character values.
- Keep
NULLwhen “unknown” is different from zero, an empty label, or a default date.
Exercise
Which function should return the first available value from mobile_phone, work_phone, and home_phone, followed by 'none'?
Show answer
Use COALESCE(mobile_phone, work_phone, home_phone, 'none').