Postgresql SQL: How check boolean field with null and True,False Value?

Postgresql 9.1

Postgresql 9.1 Problem Overview


In my database table I am having one boolean column. which have some transaction with will False, True and Null.

These are the cases i have tried:

Case:1

select * from table_name where
boolean_column is null;

works well. Give the result with all transaction having null value for that column.

Case:2

select *from table_name where boolean_column = False;

Works well. Gives result with all the transaction having False value for that column.

Case:3 This is requirement which does not works. I want all the transaction having value False and Null.

I have tried these.

i) select *from table_name where boolean_column is False or Null;

Which only gives the result for False it does not shows null records.

ii) select *from table_name where boolean_column is Null or False;

*Which only gives the result for null it does not shows records with False value. *

iii) select *from table_name where boolean_column is Null or boolean_column = False;

This is simply display all the transaction does not applied any condition at all.

How to resolve this issue. Any guidance appreciated.

Thanks in Advance.

Rgds, Anil.

Postgresql 9.1 Solutions


Solution 1 - Postgresql 9.1

There are 3 states for boolean in PG: true, false and unknown (null). Explained here: [Postgres boolean datatype][1]

Therefore you need only query for NOT TRUE:

SELECT * from table_name WHERE boolean_column IS NOT TRUE;

[1]: https://www.postgresql.org/docs/9.0/static/datatype-boolean.html "Postgress Boolean"

Solution 2 - Postgresql 9.1

I'm not expert enough in the inner workings of Postgres to know why your query with the double condition in the WHERE clause be not working. But one way to get around this would be to use a UNION of the two queries which you know do work:

SELECT * FROM table_name WHERE boolean_column IS NULL
UNION
SELECT * FROM table_name WHERE boolean_column = FALSE

You could also try using COALESCE:

SELECT * FROM table_name WHERE COALESCE(boolean_column, FALSE) = FALSE

This second query will replace all NULL values with FALSE and then compare against FALSE in the WHERE condition.

Solution 3 - Postgresql 9.1

On PostgreSQL you can use:

SELECT * FROM table_name WHERE (boolean_column IS NULL OR NOT boolean_column)

Solution 4 - Postgresql 9.1

  1. select *from table_name where boolean_column is False or Null;

    Is interpreted as "( boolean_column is False ) or (null)".

    It returns only rows where boolean_column is False as the second condition is always false.

  2. select *from table_name where boolean_column is Null or False;

    Same reason. Interpreted as "(boolean_column is Null) or (False)"

  3. select *from table_name where boolean_column is Null or boolean_column = False;

    This one is valid and returns 2 rows: false and null.

I just created the table to confirm. You might have typoed somewhere.

Solution 5 - Postgresql 9.1

Resurrecting this to post the DISTINCT FROM option, which has been around since Postgres 8. The approach is similar to Brad Dre's answer. In your case, your select would be something like

SELECT *
  FROM table_name
 WHERE boolean_column IS DISTINCT FROM TRUE
Categories
Recommended Postgresql 9.1 Solutions

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAnil KesariyaView Question on Stackoverflow
Solution 1 - Postgresql 9.1Brad DreView Answer on Stackoverflow
Solution 2 - Postgresql 9.1Tim BiegeleisenView Answer on Stackoverflow
Solution 3 - Postgresql 9.1Dmitry UkolovView Answer on Stackoverflow
Solution 4 - Postgresql 9.1Chris ChrisView Answer on Stackoverflow
Solution 5 - Postgresql 9.1shortstuffsushiView Answer on Stackoverflow