Sub topic 1
Conditions, Booleans, and NULL
Every IF, CASE, WHILE, and EXIT WHEN in PL/SQL depends on a condition. This chapter covers what a condition can evaluate to, how NULL changes the result, and how to write conditions that behave correctly when data is missing. The later chapters on IF, CASE, and loops build on these rules.
Prerequisites
This chapter assumes you can do the following:
- Write basic SQL: SELECT, WHERE, and simple DML.
- Read and write an anonymous PL/SQL block with DECLARE, BEGIN, EXCEPTION, and END.
- Declare scalar variables and assign values with
:=. - Run a script in SQL*Plus, SQLcl, or SQL Developer with
SET SERVEROUTPUT ONso thatDBMS_OUTPUTtext is displayed.
A condition has three possible results
A condition is an expression that evaluates to a BOOLEAN value. PL/SQL uses three-valued logic, so a BOOLEAN can hold one of three values:
- TRUE
- FALSE
- NULL, which means "unknown"
Comparison operators (=, <>, <, >, <=, >=), BETWEEN, IN, and LIKE all return NULL when any operand is NULL. The logic is that if you don't know the value, you can't say whether it is greater than 100.
A BOOLEAN variable that you declare without an initial value is NULL, not FALSE:
DECLARE
l_flag BOOLEAN;
BEGIN
IF l_flag THEN
DBMS_OUTPUT.PUT_LINE('THEN branch');
ELSE
DBMS_OUTPUT.PUT_LINE('ELSE branch: l_flag is NULL, not TRUE');
END IF;
END;
/Giving every BOOLEAN variable an explicit initial value avoids this surprise.
How IF reads the result
An IF statement runs its THEN branch only when the condition is TRUE. FALSE and NULL are treated the same way: control skips the THEN branch and moves to the next ELSIF or ELSE, or leaves the IF if there is none.
IF therefore cannot tell "the condition is false" from "the condition is unknown". If that difference matters to your logic, you have to test for NULL yourself.
NULL in comparisons
The following block shows the main comparison cases. It uses a small local procedure to print a BOOLEAN, because DBMS_OUTPUT.PUT_LINE does not accept BOOLEAN arguments:
DECLARE
l_bonus NUMBER := NULL;
PROCEDURE show(p_label VARCHAR2, p_value BOOLEAN) IS
BEGIN
DBMS_OUTPUT.PUT_LINE(
RPAD(p_label, 28) || ' -> ' ||
CASE
WHEN p_value THEN 'TRUE'
WHEN NOT p_value THEN 'FALSE'
ELSE 'NULL'
END);
END show;
BEGIN
show('l_bonus > 100', l_bonus > 100);
show('l_bonus = 100', l_bonus = 100);
show('l_bonus <> 100', l_bonus <> 100);
show('l_bonus = l_bonus', l_bonus = l_bonus);
show('l_bonus IS NULL', l_bonus IS NULL);
show('l_bonus IS NOT NULL', l_bonus IS NOT NULL);
END;
/The output is:
l_bonus > 100 -> NULL
l_bonus = 100 -> NULL
l_bonus <> 100 -> NULL
l_bonus = l_bonus -> NULL
l_bonus IS NULL -> TRUE
l_bonus IS NOT NULL -> FALSEThree points follow from this output:
<>is not the opposite of=when NULL is involved. Both return NULL.- A NULL is not equal to itself.
NULL = NULLis NULL. IS NULLandIS NOT NULLare the only operators that always return TRUE or FALSE for a NULL operand. Use them whenever you need to detect a missing value.
Logical operators: AND, OR, NOT
The logical operators follow the same three-valued rules. The result of AND or OR is sometimes known even when one operand is NULL, because one operand can already decide the outcome.
| Expression | Result | Reason |
|---|---|---|
| TRUE AND NULL | NULL | The result depends on the unknown value |
| FALSE AND NULL | FALSE | One FALSE decides an AND |
| TRUE OR NULL | TRUE | One TRUE decides an OR |
| FALSE OR NULL | NULL | The result depends on the unknown value |
| NOT NULL | NULL | The opposite of unknown is unknown |
Here are the same rules shown in a runnable block:
DECLARE
l_null BOOLEAN := NULL;
PROCEDURE show(p_label VARCHAR2, p_value BOOLEAN) IS
BEGIN
DBMS_OUTPUT.PUT_LINE(
RPAD(p_label, 20) || ' -> ' ||
CASE
WHEN p_value THEN 'TRUE'
WHEN NOT p_value THEN 'FALSE'
ELSE 'NULL'
END);
END show;
BEGIN
show('TRUE AND NULL', TRUE AND l_null);
show('FALSE AND NULL', FALSE AND l_null);
show('TRUE OR NULL', TRUE OR l_null);
show('FALSE OR NULL', FALSE OR l_null);
show('NOT NULL', NOT l_null);
END;
/Operator precedence applies as well. NOT binds tighter than AND, and AND binds tighter than OR. Add parentheses when a condition mixes them, so that the next reader doesn't have to remember the rules.
The NOT trap
Because NOT NULL is NULL, you cannot get the "other half" of the cases by negating a condition. This block tries both:
DECLARE
l_bonus NUMBER := NULL;
BEGIN
IF l_bonus > 100 THEN
DBMS_OUTPUT.PUT_LINE('Large bonus');
ELSIF NOT (l_bonus > 100) THEN
DBMS_OUTPUT.PUT_LINE('Small bonus');
ELSE
DBMS_OUTPUT.PUT_LINE('Neither branch ran: the condition was NULL');
END IF;
END;
/Neither the first branch nor its negation ran. If your code assumes that "not large" means "small", a NULL value falls into the gap between them and ends up in the ELSE branch. The same gap exists in a SQL WHERE clause, where rows with NULL are filtered out by both col > 100 and NOT (col > 100).
The reliable approach is to handle NULL as its own case, and to do it first:
DECLARE
l_bonus NUMBER := NULL;
BEGIN
IF l_bonus IS NULL THEN
DBMS_OUTPUT.PUT_LINE('No bonus recorded');
ELSIF l_bonus > 100 THEN
DBMS_OUTPUT.PUT_LINE('Large bonus');
ELSE
DBMS_OUTPUT.PUT_LINE('Small bonus');
END IF;
END;
/Replacing NULL before comparing
When a missing value should behave like a specific default, replace it with COALESCE (or NVL) inside the condition:
DECLARE
l_bonus NUMBER := NULL;
BEGIN
IF COALESCE(l_bonus, 0) > 100 THEN
DBMS_OUTPUT.PUT_LINE('Large bonus');
ELSE
DBMS_OUTPUT.PUT_LINE('Bonus is 100 or less, or missing');
END IF;
END;
/Use this only when the default is a meaningful business choice. Treating an unknown bonus as 0 is reasonable here. Treating an unknown credit limit as 0 could hide a data problem.
Where else NULL appears
Several Oracle-specific behaviours produce NULLs, so you should recognise them when you debug a condition:
- Empty strings are NULL. In Oracle, a zero-length VARCHAR2 is NULL. The comparison
l_text = ''is therefore never TRUE. Usel_text IS NULL. - Concatenation ignores NULL.
'Hello' || NULLreturns'Hello'. This is an exception to the rule that NULL spreads through an expression. - NOT IN with a NULL in the list never returns TRUE.
5 NOT IN (1, NULL)is NULL, because 5 might equal the unknown value. This catches many people when the list comes from a query. - Simple CASE never matches NULL.
CASE l_x WHEN NULL THEN ...compares with=, so it never fires. A later chapter covers CASE in detail. The fix is to use a searched CASE withIS NULL.
DECLARE
l_text VARCHAR2(10) := '';
BEGIN
IF l_text = '' THEN
DBMS_OUTPUT.PUT_LINE('Never printed');
END IF;
IF l_text IS NULL THEN
DBMS_OUTPUT.PUT_LINE('An empty string is NULL in Oracle');
END IF;
END;
/Writing clean conditions
- Test a BOOLEAN variable directly with
IF l_done THEN. WritingIF l_done = TRUE THENadds nothing, and the comparison is still NULL whenl_doneis NULL. - Initialize BOOLEAN variables, for example
l_done BOOLEAN := FALSE;. - Put the
IS NULLcheck first in an IF-ELSIF chain, so later branches can assume a value exists. - Prefer a positive condition to a negated one.
IF l_status = 'A'is easier to get right thanIF NOT (l_status <> 'A'), and the second form still returns NULL for a NULL status. - Remember that BOOLEAN is a PL/SQL type. Older database releases cannot use it in a SQL statement or a table column. Oracle Database 23ai adds a SQL BOOLEAN type, but conditions inside PL/SQL still follow the rules in this chapter.
Summary
A PL/SQL condition is TRUE, FALSE, or NULL. IF, and the other constructs in the next chapters, act only on TRUE. A comparison with NULL returns NULL, NOT NULL is NULL, and AND and OR return NULL unless the other operand already decides the result. Use IS NULL and IS NOT NULL to test for missing values, check for NULL before comparing, and use COALESCE when a default value is the right business behaviour.
Exercise
A block declares lqty NUMBER := NULL and runs IF lqty < 10 THEN ... ELSIF l_qty >= 10 THEN ... ELSE ... END IF. Which branch runs, and how would you restructure the IF so that a missing quantity is handled explicitly?
Show answer
The ELSE branch runs. Both lqty < 10 and lqty >= 10 evaluate to NULL, and IF treats NULL like FALSE. To handle the missing value explicitly, add IF lqty IS NULL THEN ... as the first branch, followed by the lqty < 10 and ELSE branches.
Exercise
What do these three expressions evaluate to: (1) FALSE AND NULL, (2) TRUE AND NULL, (3) NOT (NULL = NULL)?
Show answer
(1) FALSE, because one FALSE operand decides an AND. (2) NULL, because the result depends on the unknown operand. (3) NULL, because NULL = NULL is NULL and NOT NULL is NULL.