I usually use a construct like this to apply filters to columns:
CREATE TABLE test(a int NOT NULL, b int NULL)
--insert some data
INSERT INTO test(a,b) values (1,1)
INSERT INTO test(a,b) values (1,NULL)
declare @a int
declare @b int
set @a = 1
--@b is null at this time
select * from test
where a = @a
and b = isnull(@b,b)
This only selects the first row.
For example, if this was a stored procedure and argument @b was null, I could specify its value to ‘enable’ it in the where clause, and specify NULL value to select where b=b, which would be true for each column, BUT if there is a NULL value in b column, it does not get selected. Is there a standard way to deal with this? So what should I use in the above example query to select all rows even if b column has null values?
For checking NULL values you need to do it as
b is nullYou can change query as –