TATECHATLAS
◎ English
Data & databases / Guide

NULL vs Zero vs Empty String: How Oracle Handles Absence of Value

Oracle treats a zero-length character value as NULL, so you cannot distinguish an empty string from a missing value. This guide explains three-valued logic and how to write portable NULL checks.

On this page

NULL represents the absence of a known value, not a quantity of zero and not an empty string. In Oracle, a character value with length zero is treated as NULL, making the distinction between 'user left the field blank' and 'user never filled the form' impossible for VARCHAR2 columns. Arithmetic with NULL yields NULL, and comparison operators against NULL return UNKNOWN, which silently excludes rows from WHERE clauses. Only IS NULL and IS NOT NULL reliably test for absence of value. DECODE and compound keys are the sole exceptions where Oracle considers two NULLs equal.

NULL as absence of value, not a numeric or character value

A column is said to be null when it has no value for a given row. Oracle's documentation instructs you to use a null when the actual value is not known or when a value would not be meaningful. This is the conceptual foundation: null is a placeholder for missing information, not a data value that happens to look like zero or an empty string. Any column whose data type is not restricted by a NOT NULL or PRIMARY KEY constraint may contain null, and nulls can appear in columns of any data type.

The practical implication is that null participates in expressions differently from any concrete value. It does not occupy a numeric position on the number line, and it does not occupy a position in a character sequence. It signals the absence of information, and the database engine treats it as such throughout evaluation.

-- Sample table demonstrating the concept.
CREATE TABLE survey_responses (
    respondent_id NUMBER PRIMARY KEY,
    age         NUMBER,
    name        VARCHAR2(100),
    comment     VARCHAR2(4000)
);

INSERT INTO survey_responses VALUES (1, 30, 'Alice', 'Great product');
INSERT INTO survey_responses VALUES (2, NULL, NULL, NULL);   -- age unknown, name not provided
INSERT INTO survey_responses VALUES (3, 0, '', 'No comment'); -- age zero, name blank

-- Row 2: age is genuinely unknown.
-- Row 3: age is a real quantity of zero; name is an empty string.
-- In Oracle, row 3's name is stored as NULL, not as a zero-length string.

When migrating from Oracle to PostgreSQL or SQL Server, audit every WHERE column = '' clause because those databases preserve the distinction between an empty string and NULL. Replace such checks with explicit IS NULL or IS NOT NULL predicates, and consider adding a NOT NULL DEFAULT '' constraint if your schema requires an empty string to be a valid stored value distinct from absence.

Why NULL is not equivalent to zero

Zero is a valid, meaningful numeric quantity. A respondent who is zero years old has a known age; a respondent whose age is null has an unknown age. Oracle explicitly warns against using null to represent a numeric value of zero because they are not equivalent. Any arithmetic expression containing a null always evaluates to null. Adding null to 10 yields null, not 10. All operators except concatenation return null when given a null operand.

This has a direct consequence for aggregates. The AVG function ignores null values, so a null age is excluded from the mean while a zero age is included. If you mistakenly store unknown ages as zero, your average will be biased downward. If you store them as null, the average reflects only the respondents whose age is known.

-- AVG ignores NULLs but counts zeros as real values.
SELECT AVG(age) AS avg_age FROM survey_responses;
-- With rows 1,2,3 above: AVG considers 30 and 0, skips NULL.
-- Result: 15.0 (not 10.0 which would include a hypothetical NULL as zero).

-- Arithmetic with NULL propagates NULL.
SELECT age + 10 AS age_plus_10 FROM survey_responses WHERE respondent_id = 2;
-- Result: NULL, not 10.

Oracle's empty string equals NULL: a deliberate conflation

Oracle Database currently treats a character value with a length of zero as null. This means that inserting an empty string into a VARCHAR2 column stores NULL, and selecting WHERE name = '' returns no rows because the comparison evaluates to UNKNOWN against a null value. The documentation adds a forward-looking caveat: this behavior may not continue in future releases, and Oracle recommends that you do not treat empty strings the same as nulls.

In practice this means that for character columns in Oracle you cannot distinguish 'the user submitted a blank field' from 'the user never filled in the field.' Both produce NULL in storage. If your application logic requires that distinction, you must introduce a sentinel value such as a single space character or a dedicated boolean flag column.

-- In Oracle, '' IS NULL evaluates to TRUE.
SELECT CASE WHEN name IS NULL THEN 'absent' ELSE 'present' END AS status
FROM survey_responses WHERE respondent_id = 3;
-- Even though we inserted '', Oracle stored NULL.
-- Result: 'absent'

-- WHERE name = '' returns zero rows in Oracle.
SELECT COUNT(*) FROM survey_responses WHERE name = '';
-- Result: 0 (comparison with NULL yields UNKNOWN, not TRUE).

Three-valued logic: why comparison with NULL returns UNKNOWN

SQL uses three-valued logic: TRUE, FALSE, and UNKNOWN. When a comparison operator encounters a null operand and the result depends on the unknown value, the outcome is UNKNOWN rather than TRUE or FALSE. A condition that evaluates to UNKNOWN acts almost like FALSE in a WHERE clause, meaning no rows are returned. However, UNKNOWN differs from FALSE in that further operations on it propagate UNKNOWN. NOT FALSE evaluates to TRUE, but NOT UNKNOWN evaluates to UNKNOWN.

This is the mechanism that causes silent row exclusion. If you write WHERE age != 30, rows where age is null are not returned because the comparison yields UNKNOWN, not TRUE. The user expects to see all non-30 rows but gets only rows with a known age that differs from 30.

-- WHERE age != 30 silently excludes NULL ages.
SELECT respondent_id FROM survey_responses WHERE age != 30;
-- Returns respondent 3 only (age = 0).
-- Respondent 2 (age NULL) is excluded because NULL != 30 is UNKNOWN.

-- Correct query to include unknowns:
SELECT respondent_id FROM survey_responses
WHERE age != 30 OR age IS NULL;
-- Returns respondents 2 and 3.

IS NULL and IS NOT NULL: the only reliable tests

To test for nulls, Oracle documentation states that you should use only the comparison conditions IS NULL and IS NOT NULL. If you use any other condition with nulls and the result depends on the value of the null, the result is UNKNOWN. This is a structural guarantee of the SQL standard that Oracle implements without exception.

SQL Server provides a configuration toggle that changes this behavior. When SET ANSI_NULLS is OFF, the equals and not-equals operators treat NULL as a known value equivalent to any other NULL, returning TRUE or FALSE rather than UNKNOWN. Oracle does not offer this toggle; IS NULL and IS NOT NULL are always the correct approach.

-- Reliable null test in Oracle:
SELECT respondent_id FROM survey_responses WHERE name IS NULL;
-- Returns respondents 2 and 3 (both stored as NULL).

-- Unreliable test that silently fails:
SELECT respondent_id FROM survey_responses WHERE name = NULL;
-- Returns zero rows because NULL = NULL evaluates to UNKNOWN.

Where Oracle does treat two NULLs as equal: DECODE and compound keys

Although a null cannot be equal or unequal to any value or to another null in standard comparisons, Oracle provides two exceptions. The DECODE function considers two nulls to be equal. If both the expression and the search value are null, DECODE returns the corresponding result value. This makes DECODE useful for comparing nullable columns where you want two absent values to match.

The second exception involves compound keys. Oracle considers identical two compound keys containing nulls if all the non-null components of the keys are equal. This behavior supports unique constraint semantics where null components do not prevent a row from matching an existing key.

-- DECODE treats two NULLs as equal.
SELECT DECODE(a, b, 'same', 'different') AS result
FROM (
    SELECT NULL AS a, NULL AS b FROM dual
);
-- Result: 'same' (DECODE considers NULL = NULL).

-- Standard CASE does NOT: it returns 'different'.
SELECT CASE WHEN a = b THEN 'same' ELSE 'different' END AS result
FROM (
    SELECT NULL AS a, NULL AS b FROM dual
);
-- Result: 'different' (NULL = NULL is UNKNOWN, CASE falls to ELSE).

Decision framework: when to use NULL, zero, or empty string

Use NULL when the value is genuinely unknown or inapplicable to the row. Use zero when zero is a valid quantity that should participate in arithmetic and aggregation. Use an empty string only if your database preserves the distinction between an empty string and NULL, which Oracle does not for VARCHAR2 columns.

The trade-off in Oracle is that for character columns you lose the ability to distinguish 'user left field blank' from 'user did not fill the form.' If this distinction matters for your application, add a separate NOT NULL boolean column such as form_submitted or use a sentinel value. For numeric columns the decision is clearer: store zero when the quantity is known to be zero, store NULL when the quantity is unknown.

-- Schema design that preserves the distinction in Oracle:
CREATE TABLE form_data (
    id              NUMBER PRIMARY KEY,
    phone_number    VARCHAR2(20),  -- NULL means not provided
    phone_provided  NUMBER(1) NOT NULL, -- 1 means user filled the field
    age             NUMBER,        -- NULL means unknown, 0 means age is zero
    age_known       NUMBER(1) NOT NULL
);

-- Query: 'did the user submit a blank phone?' requires the flag.
SELECT * FROM form_data
WHERE phone_provided = 1 AND phone_number IS NULL;
-- In Oracle this returns rows where the user filled the field with ''
-- because '' was stored as NULL but the flag records submission.

Portability warning and forward-looking recommendation

Oracle's empty-string-as-null behavior is documented as potentially changing in future releases. SQL Server uses SET ANSI_NULLS to control whether equals and not-equals operators treat NULL as a comparable value. PostgreSQL and standard SQL preserve the distinction between an empty string and NULL. Portable code should always use IS NULL rather than = '' or = NULL to test for absence of value.

Design schemas where the semantic distinction between unknown and zero or empty is preserved through data type choice, NOT NULL constraints with defaults, or companion flag columns. Relying on the conflation of empty string and NULL creates a maintenance risk when migrating databases or when Oracle changes this behavior in a future release.

-- Portable pattern that works across Oracle, PostgreSQL, and SQL Server:
-- Always test absence with IS NULL, never with = '' or = NULL.
SELECT respondent_id FROM survey_responses WHERE name IS NULL;

-- If you need 'empty string is a valid stored value' in PostgreSQL:
-- PostgreSQL stores '' as a distinct value from NULL.
-- The same INSERT with '' would NOT become NULL there.
-- This is why cross-database code must avoid relying on the conflation.

Things to check

  • Oracle stores a zero-length VARCHAR2 value as NULL, confirmed by the documentation statement that a character value with length zero is treated as null.
  • Arithmetic with NULL yields NULL: NULL + 10 evaluates to NULL, not 10, per Oracle documentation.
  • WHERE column = NULL returns zero rows because the comparison evaluates to UNKNOWN, not TRUE.
  • IS NULL and IS NOT NULL are the only comparison conditions that reliably test for null in Oracle.
  • DECODE considers two NULLs equal; standard CASE WHEN a = b does not because NULL = NULL is UNKNOWN.
  • SQL Server SET ANSI_NULLS OFF changes = and <> to treat NULL as a comparable known value; Oracle has no equivalent toggle.
  • AVG ignores NULL values but includes zero as a real quantity, so misusing zero for unknown corrupts aggregates.
  • Oracle documentation explicitly warns that empty-string-as-null behavior may not continue in future releases.

This guide covers Oracle and SQL Server behavior only. MySQL and SQLite have different empty-string semantics not addressed here. The forward-looking note about Oracle possibly changing empty-string-as-null behavior is based on Oracle's own documentation caveat and cannot be verified as a future fact. DECODE equality for NULLs is an Oracle-specific extension and does not apply to standard CASE expressions or other databases. NULL handling in outer joins, window functions, and partitioning introduces additional edge cases beyond this scope. Tested against Oracle Database 21c documentation and SQL Server Azure SQL Database documentation as referenced.

Sources

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