Re: [SQL] nulls and datetime

From: "Gary MacMinn" <G(dot)MacMinn(at)nca(dot)gov(dot)au>
To: tgl(at)sss(dot)pgh(dot)pa(dot)us
Cc: lockhart(at)alumni(dot)caltech(dot)edu, pgsql-sql(at)postgreSQL(dot)org
Subject: Re: [SQL] nulls and datetime
Date: 1999-11-17 05:16:37
Message-ID: s832d4f1.017@syd_aux.nca.gov.au
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Tom,

Thanks for that. Coalesce worked like a ripper. It even forced me to update the database to 6.5.2. I'd been putting it off for a while.

Gary

>>> Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> 15/11/99 16:10:51 >>>
"Gary MacMinn" <G(dot)MacMinn(at)nca(dot)gov(dot)au> writes:
> I'm trying to find out if PostgreSQL has a function like NVL that
> replaces null values with a default?

NVL is not in the SQL standard. But COALESCE is, and you can get
what you want with
COALESCE(x, value-to-use-if-x-is-null)

I'd recommend updating to 6.5.2 or later before doing much with
COALESCE --- it depends on CASE which was fairly buggy in older
releases.

> I have a table with datetime fields, some of which are null. I'm
> trying to extract the date and time components with the date() and
> time() functions but the null values break these functions.

Hmmm ...

regression=> SELECT t1, date(d1) from test1;
ERROR: Unable to convert null datetime to date
regression=> SELECT t1, time(d1) from test1;
ERROR: Unable to convert null datetime to date

This is undesirable, I agree. It seems to me that those routines should
just return NULL given NULL input, rather than going out of their way to
break applications. Thomas, can you defend this behavior?

regards, tom lane

************

Browse pgsql-sql by date

  From Date Subject
Next Message Jan Wieck 1999-11-17 08:26:52 Re: [SQL] zombie postmasters
Previous Message Tom Lane 1999-11-17 05:05:27 Re: [SQL] zombie postmasters