Re: [SQL] nulls and datetime - Mailing list pgsql-sql

From Tom Lane
Subject Re: [SQL] nulls and datetime
Date
Msg-id 15251.942646251@sss.pgh.pa.us
Whole thread Raw
In response to Re: [SQL] how can tell if a column is a primary key?  ("Gary MacMinn" <G.MacMinn@nca.gov.au>)
List pgsql-sql
"Gary MacMinn" <G.MacMinn@nca.gov.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 withCOALESCE(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


pgsql-sql by date:

Previous
From: "Gary MacMinn"
Date:
Subject: [SQL] Handling NULL Dates
Next
From: Stoyan Genov
Date:
Subject: Re: [SQL] Trying to move a 6.1 database to 6.5