Re: When is a blank not a null or ''

From: Csaba Nagy <nagy(at)ecircle-ag(dot)com>
To: Alban Hertroys <alban(at)magproductions(dot)nl>
Cc: mike <mike(at)redtux1(dot)uklinux(dot)net>, Postgres general mailing list <pgsql-general(at)postgresql(dot)org>
Subject: Re: When is a blank not a null or ''
Date: 2005-02-02 13:42:52
Message-ID: 1107351771.19443.311.camel@localhost.localdomain
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

[snip]
> Or if there are also blanks among those e-mail addresses:
>
> SELECT first_name,work_email FROM tb_contacts WHERE
> tb_contacts.work_email IS NOT NULL AND tb_contacts.work_email != '';

The "tb_contacts.work_email IS NOT NULL" clause is superfluous, the
other condition will evaluate to false for null email anyway: a null
compared with any operator to any value is always null, which fails the
comparison. Generally any operator involving a null always results in
null, except a few special operators like "IS NULL" and some others.

In fewer words, the original statement will filter out both null and
empty string emails, but not emails with one or more space characters in
them. For example " " will be selected, but for a human it still looks
blank. I wonder what data type you have, cause e.g. if you have char(n),
that will be padded automatically with space characters
(see http://www.postgresql.org/docs/7.4/static/datatype-character.html)

I you do have space characters in the email field, you could use:

trim(both from tb_contacts.work_email) != ''
or
char_length(trim(both from tb_contacts.work_email)) != 0
See also:
http://www.postgresql.org/docs/7.4/static/functions-string.html

This should filter out all null, empty string, and only space emails.

HTH,
Csaba.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Berend Tober 2005-02-02 13:53:23 Re: When is a blank not a null or ''
Previous Message Martin Pitt 2005-02-02 13:15:01 Introducing the future Debian multiversion/multicluster architecture