TATECHATLAS
◎ English
Data & databases / Guide

Handling NULL in SQL: Arithmetic, Comparisons, and Oracle Concatenation

Learn how NULL propagates through arithmetic and logical operations, the unique behavior of Oracle concatenation, and the nuances of three-valued logic.

On this page

In SQL, NULL represents the absence of a value or an unknown state. It is not equivalent to zero or an empty string. Most arithmetic and logical operations involving a NULL operand result in NULL or UNKNOWN. However, Oracle Database provides a specific exception: the concatenation operator (||) treats a NULL operand as an empty string rather than propagating the NULL to the entire result. For comparisons, standard operators like = or != return UNKNOWN when compared with NULL, necessitating the use of IS NULL or IS NOT NULL. In three-valued logic, UNKNOWN behaves similarly to FALSE in a WHERE clause (returning no rows) but differs when negated, as NOT UNKNOWN remains UNKNOWN.

Understanding NULL and its Presence

A NULL value indicates that a column in a row has no value or that the value is unknown. It is a marker for missing information rather than a specific data value. It is critical to distinguish NULL from a numeric zero or an empty string, as they are not equivalent in logical evaluations.

NULL can appear in any column that is not restricted by NOT NULL or PRIMARY KEY integrity constraints. While Oracle currently treats a character value with a length of zero as NULL, this is a specific implementation detail that should not be relied upon for future-proof code, as the database documentation suggests this behavior may change in future releases.

SELECT 10 + NULL AS result FROM dual;

Always use IS NULL or IS NOT NULL for existence checks. Avoid relying on SET ANSI_NULLS OFF in SQL Server for portability, and remember that Oracle's treatment of empty strings as NULL is a legacy behavior that may change in future releases.

NULL Propagation in Arithmetic

Arithmetic operations follow a strict propagation rule: any expression containing a NULL operand will evaluate to NULL. This ensures that if one part of a calculation is unknown, the entire result remains unknown.

For example, adding 10 to a NULL value does not result in 10; it results in NULL. This applies to addition, subtraction, multiplication, and division.

SELECT 10 + NULL AS result FROM dual;

The Oracle Concatenation Exception

In Oracle Database, the concatenation operator (||) is the notable exception to the propagation rule. While almost all other operators return NULL when given a NULL operand, concatenation treats a NULL as if it were an empty string.

This behavior is highly practical for building strings from multiple columns where some fields might be optional. If you concatenate a known string with a NULL, the known string is preserved rather than the entire result becoming NULL.

SELECT 'abc' || NULL AS result FROM dual;

Three-Valued Logic and Comparisons

SQL uses three-valued logic: TRUE, FALSE, and UNKNOWN. Because NULL represents an unknown, you cannot use standard equality operators to test for it. Comparing a value to NULL using = or != will always result in UNKNOWN.

To reliably test for the presence or absence of a NULL, you must use the specific comparison conditions IS NULL or IS NOT NULL. Using any other condition where the result depends on the NULL value will yield UNKNOWN.

SELECT NULL = NULL AS equality_test, NULL != NULL AS inequality_test, NULL IS NULL AS is_null_test FROM dual;

The Behavior of UNKNOWN in WHERE Clauses

In a WHERE clause, a condition that evaluates to UNKNOWN acts similarly to FALSE in that the row is not returned. However, they are logically distinct. The primary difference appears when using the NOT operator.

If a condition is FALSE, NOT FALSE evaluates to TRUE. However, if a condition is UNKNOWN, NOT UNKNOWN evaluates to UNKNOWN. This means that UNKNOWN values can 'leak' through logical negations, unlike standard boolean logic.

-- If condition 'A' is UNKNOWN: A is TRUE -> FALSE; A is FALSE -> TRUE; A is UNKNOWN -> UNKNOWN (Row not returned); NOT A is UNKNOWN -> UNKNOWN (Row not returned)

Oracle Exceptions: DECODE and Compound Keys

Oracle provides specific contexts where NULLs are treated as equal for functional or structural purposes. In the DECODE function, Oracle considers two NULL values to be equal during evaluation.

Additionally, when evaluating compound keys, Oracle considers two keys to be identical if all their non-null components are equal, even if they contain NULLs in different positions. This is a specialized behavior for key matching that deviates from standard comparison rules.

SELECT DECODE(NULL, NULL, 'Match', 'No Match') AS result FROM dual;

SQL Server and ANSI_NULLS

SQL Server handles NULL comparisons based on the SET ANSI_NULLS configuration. When ANSI_NULLS is ON (the standard behavior), operators like = and <> return UNKNOWN when encountering NULLs.

When ANSI_NULLS is set to OFF, the behavior changes: the = and <> operators treat NULL as a known value, making them equivalent to each other. In this mode, these operators return only TRUE or FALSE, never UNKNOWN. This is a non-standard behavior and is generally discouraged for portable code.

-- SQL Server behavior (Conceptual): If ANSI_NULLS ON, SELECT CASE WHEN NULL = NULL THEN 'Yes' ELSE 'No' END returns 'No'; if ANSI_NULLS OFF, it returns 'Yes'.

Rules for NULL-Safe Code

To write robust, portable SQL, follow these guidelines: First, always use IS NULL and IS NOT NULL for existence checks. Second, do not rely on the specific concatenation behavior of Oracle if you intend to migrate to other database engines. Third, avoid using non-standard settings like ANSI_NULLS OFF in SQL Server.

Finally, when performing arithmetic on columns that allow NULLs, use functions like COALESCE or NVL to provide a default numeric value (like 0) to prevent the entire calculation from collapsing into NULL.

SELECT salary + COALESCE(commission, 0) AS total_pay FROM employees;

Things to check

  • Verify that arithmetic involving NULL results in NULL.
  • Confirm that 'column = NULL' does not return rows even if the column contains NULL.
  • Check that 'IS NULL' is used instead of '=' for null testing.
  • Ensure 'NOT UNKNOWN' is treated as UNKNOWN in logical evaluations.
  • Validate that Oracle concatenation preserves non-null strings.

The rule regarding concatenation as an exception is specific to Oracle Database. The behavior of SET ANSI_NULLS is specific to Microsoft SQL Server. Oracle's treatment of empty strings as NULL is a current implementation and may change in future versions. This guide does not cover NULL handling within specialized aggregate or window functions.

Sources

  1. Oracle Database: Nulls ↗
  2. Microsoft SQL Server: Comparison Operators ↗
Back to top ↑