1

The following query yields an error

select 1 where case null when null then true when true then true else false end

ERROR:  operator does not exist: text = boolean

What is the correct way to perform a nullable boolean value check in the WHERE clause?

Udate
Suprisingly this one works fine

create table d (id serial, v boolean);
insert into d(v) values(null),(true),(false);
select id from d where case v when null then true when v then true else true end;

 id
----
  1
  2
  3
(3 rows)

PS: actually this does not work either because the null value is casted to boolean and hence else is triggered instead of when null.

4
  • The whole expression makes no sense to me. What is the actual underlying problem you are trying to solve with that? If you want to treat null as true why not simply use where v or v is null? Commented Jun 30, 2021 at 6:51
  • I need to construct the query from within nodejs code with the value defined explisitly as null/true/false as shown in the original example. Commented Jun 30, 2021 at 6:58
  • where $1 is null or $1? with $1 being the parameter. Or better: only append the condition if the parameter is not null Commented Jun 30, 2021 at 7:00
  • Laurenz Albe's solution resolves the case approach but your alternative solution suits my case better. thank you! Commented Jun 30, 2021 at 7:46

2 Answers 2

1

You need to cast that first null to a boolean. Postgres is casting it to text, which results in your error.

select 1 where case null::boolean when null then true when true then true else false end;

That said, it might be easier to use DISTINCT FROM or NOT DISTINCT FROM depending on your actual use case.

Sign up to request clarification or add additional context in comments.

3 Comments

In addition to DISTINCT FROM, booleans have their own set of null-aware comparison operators; this specific case looks like it boils down to NULL IS NOT FALSE
if you cast null to boolean then when null condition will never trigger
@yaugenka It solves the ERROR you were getting, but it's unclear to me what you behavior you are actually looking for. Some sample data and expected results would be useful.
0

Your error stems from the fact that PostgreSQL decides on the data type for the NULL without considering the WHEN clauses. Lacking other information, it goes for text. You can prevent that with an explicit type cast to boolean.

But your real problem is different. Look at this:

SELECT CASE NULL::boolean
           WHEN NULL THEN 'is null'
           WHEN TRUE THEN 'is true'
           ELSE 'is false'
       END;

   case   
══════════
 is false
(1 row)

The problem here is that if first WHEN will evaluate NULL = NULL, which results in NULL and not in TRUE.

Either treat nullness in the ELSE branch:

CASE NULL::boolean
   WHEN TRUE THEN 'is true'
   WHEN FALSE THEN 'is false'
   ELSE 'is null'
END

or use

CASE
   WHEN NULL IS NULL THEN 'is null'
   WHEN NULL IS TRUE THEN 'is true'
   WHEN NULL IS FALSE THEN 'is false'
END

4 Comments

if you cast null to boolean then when null condition will never trigger
That's what I am saying.
so the solution is to use "case when null is null.." instead of "case null when null".
Right. Or use the ELSE branch, as stated in my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.