0

When comparing boolean values in PostgreSQL we use the comparison operator =. This is all well but when I want to check against matching NULL values what should I use?

I have used IS NOT DISTINCT FROM when comparing integers that have to match if both are NULL but is it a correct way to go when dealing with boolean values?

1
  • distinct from also works with booleans: is not distinct from true Commented Aug 1, 2016 at 13:38

3 Answers 3

1

I have used IS NOT DISTINCT FROM when comparing integers that have to match if both are NULL but is it a corract way to go when dealing with boolean values?

I see no reason why not. :)

x IS NOT DISTINCT FROM y is basically short-hand for (x = y) OR (x IS NULL AND y IS NULL)

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

Comments

0

Use is null clause. Like this:

select * from my_table where my_booleae_field is null

1 Comment

It was not the question. I don't need to get data WHERE smth is null. I need to compare argument with db value where they might be null but may be "correct" boolean values as well.
0

Try using coalesce

Like:

where coalesce(your_field, true) = true

Then null values and boolean true will pass the statement

Comments

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.