The DISTINCT
keyword comes to the rescue!
Firebird 2 and above implement IS [NOT] DISTINCT
allowing you to perform (in)equality tests that take NULL
into account.The semantics are as follows:
-
Two expressions are
DISTINCT
if they have different values or if one isNULL
and the other isn’t; -
They are
NOT DISTINCT
if they have the same value or if they are bothNULL
.
Notice that if neither operand is NULL
, IS DISTINCT
works exactly like the “<>
” operator, and IS NOT DISTINCT
like the “=
” operator.
IS DISTINCT
and IS NOT DISTINCT
always return true
or false
, never NULL
.
Using DISTINCT
, you can rewrite the first PSQL example as follows:
if (a is not distinct from b) then
MyVariable = 'Equal';
else
MyVariable = 'Not equal';
And the second as:
if (a is distinct from b) then
MyVariable = 'Not equal';
else
MyVariable = 'Equal';
These versions will give you the results that a normal (i.e. not SQL-brainwashed) human being would expect, whether there are NULL
s involved or not.