Received: from localhost (unknown [200.46.204.191]) by postgresql.org (Postfix) with ESMTP id 48A272E023E for ; Tue, 27 Nov 2007 22:48:15 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.191]) (amavisd-maia, port 10024) with ESMTP id 81396-05 for ; Tue, 27 Nov 2007 22:48:11 -0400 (AST) Received: from proton.planetargon.com (proton.planetargon.com [198.145.115.11]) by postgresql.org (Postfix) with ESMTP id 4AD482E005D for ; Tue, 27 Nov 2007 22:48:10 -0400 (AST) Received: from [10.0.1.198] (c-67-165-16-168.hsd1.ct.comcast.net [67.165.16.168]) by proton.planetargon.com (Postfix) with ESMTP id 1743894C837; Tue, 27 Nov 2007 18:47:40 -0800 (PST) In-Reply-To: <474404120711271804y21b8e0b3j7f4b3771c088a82a@mail.gmail.com> References: <474404120711271804y21b8e0b3j7f4b3771c088a82a@mail.gmail.com> Mime-Version: 1.0 (Apple Message framework v752.2) Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed Message-Id: <17DF2022-C1D2-4D9E-ABAC-DC3F5B20FBE7@seespotcode.net> Cc: pgsql-sql@postgresql.org Content-Transfer-Encoding: 7bit From: Michael Glaesemann Subject: Re: NULLIF problem Date: Tue, 27 Nov 2007 21:47:36 -0500 To: "Gera Mel Handumon" X-Mailer: Apple Mail (2.752.2) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200711/139 X-Sequence-Number: 29667 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