Thursday 20 November 2014

SQL - select bit field queries

Suppose you have a null-able bit field in your table and you have rows with true(0), false (1) and NULL values in that column.

To get all true values, the query is
SELECT * FROM Table_1 where IsActive=1

To get all false values, the query is
SELECT * FROM Table_1 where IsActive=0

To get all NULL values, the query is
SELECT * FROM Table_1 where IsActive IS NULL

To get all rows where IsActive is not true,
the query
SELECT * FROM Table_1 where IsActive<>1 will not work.
the query SELECT * FROM Table_1 where (IsActive = 0 or IsActive IS NULL) works.

No comments:

Post a Comment