Re: NULLIF problem - Mailing list pgsql-sql

From Michael Glaesemann
Subject Re: NULLIF problem
Date
Msg-id 17DF2022-C1D2-4D9E-ABAC-DC3F5B20FBE7@seespotcode.net
Whole thread Raw
In response to NULLIF problem  ("Gera Mel Handumon" <geramel.h@gmail.com>)
Responses Re: NULLIF problem
Re: NULLIF problem
List pgsql-sql
On Nov 27, 2007, at 21:04 , Gera Mel Handumon wrote:

> I encounter an error if i use NULLIF with timestamp with time zone.
> eq. dbtime=nullif(mytime,'')
>
> i want to null the value of field DBTIME if the variable mytime=" "
>
> DBTIME ="timestamp with time zone" datatype
>
> error: column DBTIME is of type timestamp with time zone but  
> expression is of type text.

I believe the reason is that '' is not a valid timestamp value: think  
of it this way:

IF mytime = '' THEN  mytime := NULL;
END IF;

The first thing it needs to do is compare the mytime value with ''.  
As '' is not a valid timestamp value, it may be casing mytime to  
text. You'll run into problems if you're assigning a text value to a  
timestamp field (which happens after the initial comparison--and the  
cast--are done.)

I think you may need to handle this is you middleware, or handle the  
IF THEN explicitly in a function. Maybe CASE would work:

CASE WHEN mytime = '' THEN NULL     ELSE CAST(mytime AS TIMESTAMP)    END

Michael Glaesemann
grzm seespotcode net




pgsql-sql by date:

Previous
From: "Gera Mel Handumon"
Date:
Subject: NULLIF problem
Next
From: Tom Lane
Date:
Subject: Re: Strang behaviour SELECT ... LIMIT n FOR UPDATE