Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 305
Anil Kesariya Avatar asked Oct 28 '15 04:10

Anil Kesariya


People also ask

Can a boolean field be NULL in Postgres?

PostgreSQL supports a single Boolean data type: BOOLEAN that can have three values: true , false and NULL .

Is NULL true in PostgreSQL?

PostgreSQL IS NOT NULL operator The expression returns true if the value is not NULL or false if the value is NULL.

Can a boolean be NULL in SQL?

You should avoid NULL for a boolean. In SQL, you cannot compare NULL with TRUE and FALSE . What should I do to avoid this problem? Well, you should add a default value to your boolean, and expect it to not be able to have a NULL value.


1 Answers

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

Therefore you need only query for NOT TRUE:

SELECT * from table_name WHERE boolean_column IS NOT TRUE; 
like image 141
Brad Dre Avatar answered Sep 18 '22 18:09

Brad Dre