Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking database for NULL boolean value

I have a field in a table that is boolean, a range of records have no value (not true or false). How do I write my SQL statement to find these?

I have tried the following SQL statements without success:

1) SELECT * FROM table WHERE field = NULL
2) SELECT * FROM table WHERE field != TRUE AND field != FALSE

Any help would be greatly appreciated.

Many Thanks, Ben

like image 601
Ben Everard Avatar asked Jun 11 '09 14:06

Ben Everard


People also ask

How do you check if a Boolean is NULL in SQL?

select * from table_name where boolean_column is null; works well. Give the result with all transaction having null value for that column.

How do you do NULL check for Boolean?

Boolean is a reference type, that's the reason you can assign null to a Boolean "variable". Example: Boolean testvar = null; if (testvar == null) { ...} Yes, because then hideInNav holds a reference to a Boolean object and can be null (no reference assigned).

Can Boolean be NULL in database?

BOOLEAN can have TRUE or FALSE values. BOOLEAN can also have an “unknown” value, which is represented by NULL. Boolean columns can be used in expressions (e.g. SELECT list), as well as predicates (e.g. WHERE clause).

Can a Boolean value be NULL?

Nullable boolean can be null, or having a value “true” or “false”. Before accessing the value, we should verify if the variable is null or not. This can be done with the classical check : if … else …


3 Answers

In TSQL, you need to use IS rather than = when comparing with NULL:

SELECT * FROM table WHERE field IS NULL
like image 185
Mitch Wheat Avatar answered Sep 30 '22 20:09

Mitch Wheat


Try

select * from table where field IS null
like image 25
Nick DeVore Avatar answered Sep 30 '22 18:09

Nick DeVore


You want IS NULL I believe:

SELECT * FROM table WHERE field IS NULL
like image 41
samjudson Avatar answered Sep 30 '22 20:09

samjudson