Boolean operations
All the operators examined so far return NULL if any operand is NULL.With boolean operators, things are a bit more complex:
-
not NULL = NULL -
NULL or false = NULL -
NULL or true = true -
NULL or NULL = NULL -
NULL and false = false -
NULL and true = NULL -
NULL and NULL = NULL
In version 2.5 and earlier, Firebird SQL doesn’t have a boolean data type;nor are true and false existing constants.In the leftmost column of the explanatory table below, “true” and “false” represent expressions (fields, variables, composites…) that evaluate to true/false.
If A is NULL, then: |
Is: | Because: |
|---|---|---|
|
|
If |
|
|
“ |
|
|
“ |
|
|
“ |
|
|
“ |
|
|
“ |
|
|
“ |
All these results are in accordance with boolean logic.The fact that you don’t need to know X's value to compute “X or true” and “X and false” is also the basis of a feature found in various programming languages: short-circuit boolean evaluation.
The above results can be generalised as follows for expressions with one type of binary boolean operator (and | or) and any number of operands:
- Disjunctions (“
A or B or C or D or …”) -
-
If at least one operand is
true, the result istrue. -
Else, if at least one operand is
NULL, the result isNULL. -
Else (i.e. if all operands are
false) the result isfalse.
-
- Conjunctions (“
A and B and C and D and …”) -
-
If at least one operand is
false, the result isfalse. -
Else, if at least one operand is
NULL, the result isNULL. -
Else (i.e. if all operands are
true) the result istrue.
-
Or, shorter:
-
TRUEbeatsNULLin a disjunction (OR-operation); -
FALSEbeatsNULLin a conjunction (AND-operation); -
In all other cases,
NULLwins.
If you have trouble remembering which constant rules which operation, look at the second letter: tRue prevails with oR — fAlse with And.