Re: + operator with a possible NULL operand

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Michael Glaesemann <grzm(at)myrealbox(dot)com>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: + operator with a possible NULL operand
Date: 2003-05-29 06:38:31
Message-ID: 15078.1054190311@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Michael Glaesemann <grzm(at)myrealbox(dot)com> writes:
> SELECT a.qty AS aa,
> b.qty AS bb,
> a.qty + b.qty AS cc
> FROM ...

> The select statement seems to work just fine, except that where a.qty
> or b.qty are NULL, cc is also NULL.

Yup, that's how it should be according to the SQL spec. NULL is
effectively "unknown", so the result of adding it to anything else
is also unknown, ie NULL.

If you'd prefer to substitute something else, for either a.qty or b.qty
individually or the complete sum, see the COALESCE function.
"COALESCE(foo, bar)" means "if foo is not null then foo else bar".
(No, it's not a well-chosen function name ... we can blame the SQL
spec for this too ...)

regards, tom lane

In response to

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Vincent Hikida 2003-05-29 06:43:19 Re: + operator with a possible NULL operand
Previous Message Michael Glaesemann 2003-05-29 05:58:05 + operator with a possible NULL operand