Re: Problem using NULLIF in a CASE expression

From: Richard Huxton <dev(at)archonet(dot)com>
To: Bruno BAGUETTE <levure(at)baguette(dot)net>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Problem using NULLIF in a CASE expression
Date: 2005-09-09 12:36:47
Message-ID: 432181DF.4080707@archonet.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Bruno BAGUETTE wrote:
>
> 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

> Why this query does not accept the NULLIF ?

It's not the NULLIF, it's the "WHEN NOT NULL". If you reverse the logic
of the case it works:
SELECT CASE (nullif(btrim(' ',' '), ''))
WHEN NULL THEN 'a'::text
ELSE 'b'::text
END AS test;

I think it's because (NOT NULL) is typed as a boolean (because that's
what the NOT operator returns) and you're comparing it to the text
output of your NULLIF(...). Don't forget the WHEN clause is supposed to
have a value attached (although of course NULL complicates matters).

I'd say the better solution is to clean up the data though. Add a BEFORE
INSERT/UPDATE trigger that corrects the bad applications and then you
won't have to jump through these hoops. If the varchar should be null or
have non-space content then enforce it!
--
Richard Huxton
Archonet Ltd

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Michael Fuhr 2005-09-09 12:45:01 Re: Is this a bug or am I doing something wrong?
Previous Message Roman Neuhauser 2005-09-09 12:35:34 constraints on composite types