pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Michael Glaesemann <grzm@seespotcode.net>
To: Gera Mel Handumon <geramel.h@gmail.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: NULLIF problem
Date: Tue, 27 Nov 2007 21:47:36 -0500
Message-ID: <17DF2022-C1D2-4D9E-ABAC-DC3F5B20FBE7@seespotcode.net> (raw)
In-Reply-To: <474404120711271804y21b8e0b3j7f4b3771c088a82a@mail.gmail.com>
References: <474404120711271804y21b8e0b3j7f4b3771c088a82a@mail.gmail.com>


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





view thread (5+ messages)  latest in thread

Message-ID: <17DF2022-C1D2-4D9E-ABAC-DC3F5B20FBE7@seespotcode.net>
Permalink:  ../17DF2022-C1D2-4D9E-ABAC-DC3F5B20FBE7@seespotcode.net/
Also on:    postgresql.org/message-id/17DF2022-C1D2-4D9E-ABAC-DC3F5B20FBE7@seespotcode.net

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: grzm@seespotcode.net, geramel.h@gmail.com
  Subject: Re: NULLIF problem
  In-Reply-To: <17DF2022-C1D2-4D9E-ABAC-DC3F5B20FBE7@seespotcode.net>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox