ANSI SQL defines NULL as nothing else β not even another instance of NULL. The canonical way around this is to use IS NULL and IS NOT NULL .
There is also an MS SQL Server SET ANSI_NULLS . Turning this option off causes WHERE x = NULL do exactly what you expect. It will also, however, include any NULL values ββin a query of type WHERE x <> 'abc' - which may not be what you expect. This parameter is specific to the connection, so changing it for your connection will not affect others. You can also set the default setting at the database level. When creating a stored procedure, the parameter is recorded at creation time - not the execution time.
Another trick is building a query like WHERE ISNULL(x, '') = ISNULL(@x, '') . I donβt think it is SARGable, therefore the performance is not as good as WHERE (x IS NULL AND @x IS NULL) OR (x = @x) , but it is much more pleasant to write and build dynamically.
Oh - and since we're talking about ANSI SQL. The ANSI SQL ISNULL is COALESCE .
Mark brackett
source share