| Firebird Docset → Firebird Database Docs → Firebird Null Guide → NULL in if statements |
![]() |
If the test expression of an if statement resolves to NULL, the then clause is skipped and the else clause (if present) executed. But beware! The expression may behave like false in this case, but it doesn't have the value false. It's still NULL, and weird things may happen if you forget that. The following examples explore some of the fiendish workings of NULL in if statements:
if (a = b) then MyVariable = 'Equal'; else MyVariable = 'Not equal';
If a and b are both NULL, MyVariable will yet be “Not equal” after executing this code. The reason is that the expression “a = b” yields NULL if at least one of them is NULL. With the test expression NULL, the then block is skipped, and the else block executed.
if (a <> b) then MyVariable = 'Not equal'; else MyVariable = 'Equal';
Here, MyVariable will be “Equal” if a is NULL and b isn't, or vice versa. The explanation is analogous to that of the previous example.
if (not (a <> b)) then MyVariable = 'Equal'; else MyVariable = 'Not equal';
This one looks like it should give the same results as the previous example, doesn't it? After all, we've inverted the test expression and swapped the then and else clauses. And indeed, as long as neither variable is NULL, both code fragments are equivalent. But as soon as a or b becomes NULL, so does the entire test expression, the else clause is executed, and the result is “Not equal”.
Of course we're aware that this third example is fully equivalent to the first. We've merely included it to stress once more that not(NULL) is NULL. So, in situations where the test expression resolves to NULL, not() doesn't invert it.
| Firebird Docset → Firebird Database Docs → Firebird Null Guide → NULL in if statements |