Sub topic 5
Replacing NULL with NVL
This chapter assumes you can write basic SELECT statements, use WHERE and column aliases, recognize Oracle types such as NUMBER and VARCHAR2, query DUAL, and use simple aggregate functions. You will use that knowledge to replace missing values in expressions and reports.
The working model
NVL is an Oracle function that returns a fallback value when an expression is NULL.
NVL(expression, replacement_value)Oracle evaluates the first argument:
- If it is not
NULL,NVLreturns it. - If it is
NULL,NVLreturns the second argument.
The replacement can be a literal, a column, or another expression.
SELECT NVL(NULL, 0) AS zero_value,
NVL('ready', 'waiting') AS text_value
FROM dual;The result is 0 for zero_value and ready for text_value.
A common use is calculating pay when an employee has no commission. Without a replacement, arithmetic with NULL produces NULL.
SELECT employee_id,
salary,
commission_pct,
salary + salary * commission_pct AS total_without_nvl,
salary + salary * NVL(commission_pct, 0) AS total_pay
FROM employees;For an employee whose commission_pct is NULL, total_without_nvl is also NULL. NVL(commission_pct, 0) changes the missing commission to zero, so total_pay equals the salary.
Choosing a suitable replacement
The replacement should represent the meaning of the missing value. Use 0 when missing means no amount, but do not use it when missing means “not measured.” A default can change the business meaning of a report.
| Expression | Replacement | Typical meaning |
|---|---|---|
| `commission_pct` | `0` | No commission is paid |
| `discount_code` | `'NONE'` | No discount was selected |
| `last_login` | `SYSDATE` | Missing means the current date, if that rule is intentional |
Data types and common errors
Oracle must produce one data type for the result. Keep the two arguments compatible.
For numeric data, use a numeric replacement:
SELECT NVL(commission_pct, 0) AS commission_pct
FROM employees;For text output, convert the numeric expression first:
SELECT NVL(TO_CHAR(commission_pct), 'none') AS commission
FROM employees;This avoids asking Oracle to use the text 'none' as a number. A query such as the following can fail with a numeric conversion error:
SELECT NVL(commission_pct, 'none') AS commission
FROM employees;The exact conversion behavior depends on the first argument and Oracle's implicit conversion rules. Explicit conversion with TO_CHAR, TO_NUMBER, or another conversion function makes your intention clearer.
Another common error is replacing NULL before an aggregate without considering the result. AVG(commission_pct) ignores missing commissions. AVG(NVL(commission_pct, 0)) includes them as zero, producing an average across all employees instead of an average among employees with commissions.
A practical pattern
Use NVL at the point where a missing value needs a defined meaning:
SELECT employee_id,
NVL(commission_pct, 0) AS commission_pct,
salary * (1 + NVL(commission_pct, 0)) AS pay_factor
FROM employees
WHERE department_id = 80;This keeps the original column available while creating a safe value for the calculation.
Exercise
Write a query against DUAL that returns 10 when the first expression is NULL, and explain why the result is not NULL.
Show answer
SELECT NVL(NULL, 10) AS result FROM dual; returns 10 because NVL returns its second argument when its first argument is NULL.