Databasen - NULL dates

Andy Wardley abw at
Mon Oct 23 10:55:14 BST 2006

Ovid wrote:
> As your business rules get more complex, the more NULLs you have in a
> database, the more likely it is that your queries will return incorrect
> results.  

I've always adopted the policy that if a column can be NULL then the query
should explicitly account for that:

   where = i.order_id
     and  ( and <> '25122007')
      or  ( and and <>;

The same is true in Perl.  If a value can be undefined then any test should
begin with a clause that asserts that the value is defined.

    if ((defined $i_date && $i_date ne '25122007')
    ||  (defined $i_date && defined $o_date && $i_date ne $o_date)) {

Otherwise you'll get "undefined value" warnings (at best) or unpredictable
results (at worst).

So unless I've missed something, it suggests that it's not so much a case
of NULL dates being at fault per se, but queries that fail to account for
the fact that a date (or any other value) can be NULL.

 > This isn't to say I haven't used NULL columns in databases, but you
 > have to try and judge whether potentially incorrect queries will cost
 > more than the extra effort to avoid NULLs.

Ack.  It's one thing saying "It's OK to use NULL dates if your queries are
written correctly" and another to know that every person who may ever write
queries for the DB understands that.


More information about the mailing list