Received: from localhost (unknown [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id F2F702E3924 for ; Thu, 29 Nov 2007 05:44:01 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-maia, port 10024) with ESMTP id 71623-08 for ; Thu, 29 Nov 2007 05:43:54 -0400 (AST) Received: from wa-out-1112.google.com (wa-out-1112.google.com [209.85.146.178]) by postgresql.org (Postfix) with ESMTP id C902C2E391D for ; Thu, 29 Nov 2007 05:43:39 -0400 (AST) Received: by wa-out-1112.google.com with SMTP id m38so2373632waf for ; Thu, 29 Nov 2007 01:43:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:references; bh=omzoYhbQCUT4iJ+yIWD3eKPyYvygfs5GTUbFrd5PJXc=; b=CkhyevY7DHms/46dzP/bCaGsYKy+Ztxiirkxap9zjmTPaiKOag0MGPAit/sjrF/7rPl0FO2dlx3nRvg+UtDb80sab0ooKHlQuMDDeh/jkgzhT3dUP1pZWoQb7aLI//ff6ZPWk+8ZDnDKMcor9lUvPMvv7oc2zU4zsCIkcPX/T2Y= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:references; b=l61rp1vY1qoOs/asE91w3kqdnoVxAAv+N1LYmcQqcSawOf7kcY9YaeyAbINZBi+9c7XzImfBMPYDxFW/U0qsGEpDASvsd0Y0bcY9ZzG+RoEbYaJfz+xkFifunpULzYItw6ecGDOGFcwRnAQO1N0GFPq/gpT85oK6BOXf3cR+Cw8= Received: by 10.114.120.1 with SMTP id s1mr689037wac.1196329418361; Thu, 29 Nov 2007 01:43:38 -0800 (PST) Received: by 10.115.88.17 with HTTP; Thu, 29 Nov 2007 01:43:38 -0800 (PST) Message-ID: <474404120711290143g6ef15a73j9363b2b95882f043@mail.gmail.com> Date: Thu, 29 Nov 2007 17:43:38 +0800 From: "Gera Mel Handumon" To: "Michael Glaesemann" Subject: Re: NULLIF problem Cc: pgsql-sql@postgresql.org In-Reply-To: <17DF2022-C1D2-4D9E-ABAC-DC3F5B20FBE7@seespotcode.net> MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_1120_12121189.1196329418310" References: <474404120711271804y21b8e0b3j7f4b3771c088a82a@mail.gmail.com> <17DF2022-C1D2-4D9E-ABAC-DC3F5B20FBE7@seespotcode.net> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200711/150 X-Sequence-Number: 29678 ------=_Part_1120_12121189.1196329418310 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline Thanks! On Nov 28, 2007 10:47 AM, Michael Glaesemann wrote: > > 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 > > > -- Gera Mel E. Handumon Application Programmer PaySoft Solutions, Inc. ----------------------------------------------------------------- "Share your knowledge. It's a way to achieve immortality" - Dalai Lama ------=_Part_1120_12121189.1196329418310 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline Thanks!

On Nov 28, 2007 10:47 AM, Michael Glaesemann <grzm@seespotcode.net> wrote:

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





--
Gera Mel E. Handumon
Application Programmer
PaySoft Solutions, Inc.
-----------------------------------------------------------------
"Share your knowledge. It's a way to achieve immortality" - Dalai Lama ------=_Part_1120_12121189.1196329418310--