- •Firebird Null Guide
- •Table of Contents
- •Introduction
- •What is NULL?
- •NULL in expressions
- •Expressions returning NULL
- •NULL in boolean expressions
- •More logic (or not)
- •NULL in aggregate functions
- •NULL handling in UDFs
- •NULL <-> non-NULL conversions you didn't ask for
- •Being prepared for undesired conversions
- •More on UDFs
- •NULL in if statements
- •Testing if something is NULL
- •Setting a field or variable to NULL
- •Dealing with NULLs
- •Testing for NULL - if it matters
- •Finding out if fields are the same
- •Finding out if a field has changed
- •Substituting NULL with a value
- •The COALESCE function
- •Firebird 1.0: the *NVL functions
- •Summary
- •Document history
- •License notice
Firebird Null Guide
The correct way to test for NULL is:
...is null / ...is not null
These tests always return true or false - no messing around. Examples:
•if (MyField is null) then...
•select * from Pupils where PhoneNumber is not null
•select * from Pupils where not (PhoneNumber is null) /* does the same as the previous example */
•update Numbers set Total = A + B + C where A + B + C is not null
You could say that whereas “ = ” (when used as an equality operator) can only compare values, “ is ” tests for a state.
Setting a field or variable to NULL
Fields and variables can be set to NULL using the same syntax as for regular values:
•insert into MyTable values (1, 'teststring', NULL, '8-May-2004')
•update MyTable set MyField = null where YourField = -1
•if (Number = 0) then MyVariable = null;
- “Wait a minute... and you said that MyField = NULL was illegal!”
That's right... for the comparison operator “=” (at least in pre-2.0 versions of Firebird). But here we are talking about “=” as an assignment operator. Unfortunately, both operators have the same symbol in SQL. In assignments, whether done with “=” or with an insert list, you can treat NULL just like any value - no special syntax needed (or indeed possible).
Dealing with NULLs
This section contains some practical tips and examples that may be of use to you in your everyday dealings with NULLs.
Testing for NULL - if it matters
Quite often, you don't need to take special measures for fields or variables that may be NULL. For instance, if you do this:
select * from Customers where Town = 'Ralston'
you probably don't want to see the customers whose town is unspecified. Likewise:
if (Age >= 18) then CanVote = 'Yes'
9
