Re: Problem using NULLIF in a CASE expression

From: Csaba Nagy <nagy(at)ecircle-ag(dot)com>
To: Bruno BAGUETTE <levure(at)baguette(dot)net>
Cc: Postgres general mailing list <pgsql-general(at)postgresql(dot)org>
Subject: Re: Problem using NULLIF in a CASE expression
Date: 2005-09-09 12:13:26
Message-ID: 1126268006.3026.50.camel@coppola.muc.ecircle.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

You're misusing the case construct, and try to compare a text result
(from the 'nullif') with a boolean result (from the 'not null'). You
probably should use the other form of case:

SELECT CASE WHEN NULLIF(btrim(' A string', ' '), ') IS NULL THEN NULL
ELSE 6 END AS type_id;

I guess you were looking for something like:

db=> SELECT CASE WHEN NULLIF(btrim(' A string', ' '), '') IS NULL THEN
NULL ELSE 6 END AS type_id;
type_id
---------
6
(1 row)

db=> SELECT CASE WHEN NULLIF(btrim(' ', ' '), '') IS NULL THEN NULL ELSE
6 END AS type_id;
type_id
---------

(1 row)

Cheers,
Csaba.

On Fri, 2005-09-09 at 13:45, Bruno BAGUETTE wrote:
> Hello,
>
> I have a problem using NULLIF inside a CASE expression.
>
> I explain the goal of my query : I have a table where there is two
> fields : one varchar field and one INT8 field.
> When the varchar field is empty I have to display the INT8 field.
> If the varchar field is not empty I have to display a NULL value (even
> if the INT8 field contains something).
>
> BUT : that table is used by several applications and theses applications
> does not always store NULL in the varchar field. Some applications store
> an empty string '', or stores <space> char(s).
> SO, I have to do a btrim and to get a NULL value when I just have ''
> after the btrim.
>
> I wrote another (and quite shorter!) SQL query to resume the problem :
>
> SELECT CASE NULLIF(btrim(' A string', ' '), '')
> WHEN NOT NULL
> THEN NULL
> ELSE 6
> END AS type_id;
>
> ERROR: operator does not exist: text = boolean
> HINT: No operator matches the given name and argument type(s). You may
> need to add explicit type casts.
>
> Why this query does not accept the NULLIF ?
> What can I do to solve that problem ?
>
> Thanks in advance !
>
> Bruno BAGUETTE.
>
> ---------------------------(end of broadcast)---------------------------
> TIP 3: Have you checked our extensive FAQ?
>
> http://www.postgresql.org/docs/faq

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Roger Hand 2005-09-09 12:14:56 Re: Problem using NULLIF in a CASE expression
Previous Message Bruno BAGUETTE 2005-09-09 11:45:19 Problem using NULLIF in a CASE expression