frontin frontin - 1 month ago 7
SQL Question

associate nulls as false on filter

I have a table

tbl
that has a column
IsLib
that is bit but also accepts nulls and it has to be this way. I have a query that brings in a parameter
@bitIsLib
that is true or false and I need to filter
tbl
based on this. I cant do this:

SELECT * FROM tbl WHERE IsLib = @bitIsLib


because I need to treat nulls as false and they will be excluded from this filter. How can I write it so if
@bitIsLib
is true then it includes true values, and if
@bitIsLib
is false then it includes false and null values?

Answer
SELECT * FROM tbl WHERE ISNULL(IsLib, 0) = @bitIsLib