Sub topic 8
Matching Data Types of Replacement Values
This chapter assumes you can write basic SELECT statements, know Oracle types such as NUMBER, VARCHAR2, and DATE, and have used NVL, NVL2, and COALESCE at a basic level.
A replacement value must be compatible with the value it replaces. Oracle may perform an implicit conversion when types differ. Implicit conversion means Oracle converts a value automatically. This can make a query work, fail, or depend on session settings such as the numeric or date format.
How NVL chooses a data type
NVL(expr1, expr2) returns expr1 when it is not NULL. Otherwise, it returns expr2.
Oracle uses the data type of expr1 as an important part of its conversion rules:
- If
expr1is character data, Oracle convertsexpr2to character data. - If
expr1is numeric, Oracle converts the other argument to a compatible numeric type. - If the values cannot be converted, the statement raises an error.
For example, commission_pct is a NUMBER. Replacing it with the text 'none' asks Oracle to convert 'none' into a number. That conversion cannot succeed.
-- This can raise ORA-01722 because 'none' is not a valid number.
-- SELECT NVL(commission_pct, 'none')
-- FROM employees;
-- Convert the number to text before using a text replacement.
SELECT employee_id,
NVL(TO_CHAR(commission_pct), 'none') AS commission
FROM employees;TO_CHAR explicitly converts a value to text. Explicit conversion is safer because the query states exactly what conversion should happen.
For numeric output, use a numeric replacement:
SELECT employee_id,
salary + salary * NVL(commission_pct, 0) AS total_pay
FROM employees;Here, 0 is a number, so it is suitable for a numeric calculation.
How COALESCE checks its arguments
COALESCE returns the first non-NULL expression:
SELECT COALESCE(mobile_phone,
work_phone,
home_phone,
'no phone on file') AS best_phone
FROM contacts;All possible return values should belong to compatible data types. In this example, the phone columns and the fallback text are character values.
For numeric expressions, keep every fallback numeric:
SELECT employee_id,
COALESCE(commission_pct, 0) AS commission_pct
FROM employees;Do not mix a number with a text label when the result must remain numeric. If you need a label, convert the number to text explicitly:
SELECT employee_id,
COALESCE(TO_CHAR(commission_pct), 'none') AS commission
FROM employees;The result is now text for every row.
| Desired result | Expression pattern | Result type |
|---|---|---|
| Numeric calculation | `NVL(number_column, 0)` | Numeric |
| Text display | `NVL(TO_CHAR(number_column), 'none')` | Character |
| Date fallback | `NVL(date_column, DATE '2026-01-01')` | Date |
NVL2 follows the same principle
NVL2(expr1, expr2, expr3) returns expr2 when expr1 is not NULL. It returns expr3 when expr1 is NULL.
The two result expressions should also be compatible:
SELECT employee_id,
NVL2(commission_pct,
'Salary + commission',
'Salary only') AS pay_type
FROM employees;Both possible results are character values. If one branch is numeric and the other is text, Oracle may attempt an implicit conversion or reject the expression. Make the intended result type clear by converting one branch explicitly.
A practical decision rule
Use these steps when choosing replacement values:
- Identify the type of the original expression.
- Decide whether the result should be numeric, text, or a date.
- Use a replacement with that type.
- Add an explicit conversion when displaying one type as another.
- Test values that include both
NULLand non-NULLrows.
Exercise
commission_pct is numeric, but a report must display none when it is NULL. Which expression should you use?
Show answer
Use NVL(TO_CHAR(commission_pct), 'none'), because both possible results are text.