Sub topic 7
Returning the First Non-NULL Value with COALESCE
Assume you know basic SELECT statements, WHERE clauses, Oracle data types, the DUAL table, and aggregate functions. This chapter focuses on choosing the first available value from several alternatives.
What COALESCE does
COALESCE returns the first expression in its list that is not NULL:
COALESCE(expression_1, expression_2, expression_3, ...)Oracle checks the expressions from left to right. If every expression is NULL, the result is NULL.
SELECT COALESCE(NULL, NULL, 'fallback') AS result
FROM dual;Result:
| RESULT |
|---|
| fallback |
A common use is selecting the best available contact method:
SELECT contact_id,
COALESCE(mobile_phone,
work_phone,
home_phone,
'no phone on file') AS best_phone
FROM contacts;For each contact, Oracle tries mobile_phone first. If it is NULL, Oracle tries work_phone, then home_phone. The final text is used only when all three phone columns are NULL.
Oracle treats an empty character string as NULL. Therefore, an empty value does not count as an available value for COALESCE.
How Oracle evaluates the list
COALESCE uses short-circuit evaluation. Oracle stops after finding the first non-NULL expression, so later expressions do not need to be evaluated.
This matters when later expressions are expensive or could cause an error:
SELECT COALESCE('already available',
TO_CHAR(1 / 0)) AS result
FROM dual;The result is already available. Oracle does not need to evaluate the second expression.
Do not use short-circuit behavior as a replacement for clear query design. Keep expressions understandable, and avoid hiding important errors inside fallback expressions.
Data types and conversion
The expressions supplied to COALESCE must produce compatible result types. When the expressions are numeric, Oracle applies numeric precedence. When they are character values, Oracle uses character conversion rules.
Make conversions explicit when the fallback has a different type:
SELECT COALESCE(TO_CHAR(commission_pct), 'none') AS commission
FROM employees;Here, both arguments are character values. Without TO_CHAR, a text fallback such as 'none' can cause an invalid-number error when Oracle attempts to convert it to a number.
For dates, format the date as text only when the result is meant for display:
SELECT COALESCE(TO_CHAR(start_date, 'YYYY-MM-DD'),
'date not recorded') AS displayed_start_date
FROM employees;The result of this expression is text, not a DATE. Keep the original date type when the value will later be sorted, compared, or used in date arithmetic.
COALESCE compared with NVL
NVL accepts exactly two arguments. COALESCE accepts two or more and is usually clearer when there are several fallbacks.
| Need | Suitable function |
|---|---|
| Replace one nullable value with one fallback | `NVL` |
| Choose between a nullable value and several fallbacks | `COALESCE` |
| Return one result for `NULL` and another for non-`NULL` | `NVL2` |
For two compatible values, either function may be appropriate:
SELECT NVL(work_phone, 'no phone on file') AS phone
FROM contacts;
SELECT COALESCE(work_phone, 'no phone on file') AS phone
FROM contacts;Use COALESCE when the query expresses a priority-ordered list. It also maps naturally to standard SQL and short-circuits its arguments.
Exercise
What does COALESCE(NULL, '', 'backup') return in Oracle?
Show answer
It returns 'backup' because Oracle treats the empty string as NULL.