Sub topic 3
How NULL Propagates Through Arithmetic and Concatenation
This chapter assumes that you can write basic SELECT statements, use column aliases, and recognize Oracle NUMBER and VARCHAR2 values. You should also know that NULL represents a missing, unknown, or inapplicable value. It is not the same as zero, an empty string, or a blank space.
NULL in arithmetic
Oracle treats an arithmetic expression containing NULL as NULL. The database cannot calculate a definite result when one required value is unknown.
SELECT 100 + NULL AS addition_result,
100 * NULL AS multiplication_result,
NULL / 10 AS division_result
FROM dual;Each result is NULL. The value NULL does not mean that Oracle calculated zero. It means that the result is unknown.
This behavior affects expressions built from table columns. Suppose an employee has a salary but no commission percentage:
WITH sample AS (
SELECT 5000 AS salary,
CAST(NULL AS NUMBER) AS commission_pct
FROM dual
)
SELECT salary,
commission_pct,
salary + salary * commission_pct AS total_pay
FROM sample;total_pay is NULL because the expression includes commission_pct. If the business rule says that a missing commission means zero commission, replace the missing value before doing the calculation:
WITH sample AS (
SELECT 5000 AS salary,
CAST(NULL AS NUMBER) AS commission_pct
FROM dual
)
SELECT salary + salary * NVL(commission_pct, 0) AS total_pay
FROM sample;This query returns 5000.
NULL in concatenation
Concatenation joins character values with the || operator. Oracle handles NULL differently for concatenation than for arithmetic.
When one side is a non-NULL string, Oracle treats the NULL side as an absent string:
SELECT 'Oracle' || NULL AS left_null,
NULL || 'SQL' AS right_null,
'Oracle' || ' ' || NULL AS name_example
FROM dual;The first result is Oracle, the second is SQL, and the third is Oracle with a trailing space. The NULL does not make the whole expression NULL.
This behavior can produce labels that look correct but contain unwanted spaces. It can also hide missing data. For example:
WITH person AS (
SELECT 'Ada' AS first_name,
CAST(NULL AS VARCHAR2(20)) AS last_name
FROM dual
)
SELECT first_name || ' ' || last_name AS display_name,
first_name || ' ' || NVL(last_name, '[unknown]') AS explicit_name
FROM person;display_name contains Ada followed by a space. explicit_name makes the missing last name visible as [unknown].
Oracle also treats a zero-length character string such as '' as NULL in SQL. Therefore, using '' does not create a distinct empty text value that can be tested separately from NULL.
Common errors
- Assuming
NULL + 0becomes zero. It remainsNULL. - Assuming concatenating any
NULLmakes the entire stringNULL. Oracle preserves the non-NULLtext. - Using
''when you need a value distinct fromNULL. Oracle treats the empty string asNULL. - Replacing
NULLwithout deciding whether the replacement is semantically correct.
Exercise
What does this query return, and why? SELECT 'A' || NULL || 'B' AS result FROM dual;
Show answer
It returns AB. Oracle treats the NULL operand as an absent string when concatenating it with non-NULL text.