Sub topic 6
Branching on NULL with NVL2
Assume you know basic SELECT statements, WHERE clauses, column aliases, Oracle data types, the DUAL table, and aggregate functions. This chapter focuses on choosing between two results based on whether an expression is NULL.
What NVL2 does
NVL2 is an Oracle function that checks one expression and returns one of two values:
NVL2(expression, value_if_not_null, value_if_null)- If
expressionis notNULL, Oracle returnsvalue_if_not_null. - If
expressionisNULL, Oracle returnsvalue_if_null.
The first argument is the condition being checked. The other two arguments are results, not conditions.
SELECT NVL2(NULL, 'present', 'missing') AS null_case,
NVL2('value', 'present', 'missing') AS value_case
FROM dual;The result is missing for the first expression and present for the second.
A practical example
Suppose employees.commission_pct is NULL when an employee has no commission. You can label each employee's pay arrangement without writing a longer conditional expression:
SELECT employee_id,
salary,
commission_pct,
NVL2(
commission_pct,
'Salary plus commission',
'Salary only'
) AS pay_type
FROM employees;For a row where commission_pct is 0.10, NVL2 returns Salary plus commission. For a row where commission_pct is NULL, it returns Salary only.
NVL2 checks only whether the first expression is null. It does not test whether a number is positive, whether a string has a particular value, or whether a date is in a range. Use CASE for those more complex conditions.
NVL2 compared with NVL
NVL replaces a null value with one replacement:
NVL(commission_pct, 0)NVL2 chooses between two results:
NVL2(commission_pct, 'Commission applies', 'No commission')| Function | Main question | Number of result choices |
|---|---|---|
| `NVL` | What value should replace `NULL`? | 1 replacement |
| `NVL2` | Which result should I return for `NULL` or non-`NULL`? | 2 results |
| `COALESCE` | Which expression is the first non-`NULL` value? | Many values |
Choose NVL2 when the output is a direct two-way choice. Choose NVL when you need a value that can continue in arithmetic or another expression.
Result data types
The two result arguments should be compatible. For example, returning text from both branches is straightforward:
SELECT employee_id,
NVL2(commission_pct, 'Has commission', 'No commission') AS status
FROM employees;If one branch is numeric and the other is text, Oracle may attempt an implicit conversion. Implicit conversion means Oracle changes a value's type automatically. It can fail or produce behavior that depends on the data.
Convert values explicitly when the branches need a common type:
SELECT employee_id,
NVL2(commission_pct,
TO_CHAR(commission_pct),
'none') AS commission_text
FROM employees;Here both possible results are character values.
Common errors
- Reversing the second and third arguments.
- Treating
0asNULL. In Oracle, zero is a number, soNVL2(0, 'value', 'missing')returnsvalue. - Expecting an empty string to count as present. Oracle treats a zero-length character string as
NULL. - Using NVL2 when several fallback values are needed. Use
COALESCEfor that case.
Exercise
What does this expression return when commissionpct is NULL: NVL2(commissionpct, salary * commission_pct, salary)?
Show answer
It returns salary, because the first argument is NULL and NVL2 selects its third argument.