I am surprised to see that IS NULL and =NULL are yielding different results in a select query. What is difference between them? When to use what. I would be glad if you can explain me in detail.
In MSSQL IS NULL
is the way to say what you want. = NULL
is pretty much never what you mean (and it's really anoying some times).
Check it,
http://www.sqlservercentral.com/articles/T-SQL/understandingthedifferencebetweenisnull/871/
= NULL
is always unknown
(this is piece of 3 state logic), but WHERE
clause treats it as false
and drops from the result set. So for NULL
you should use IS NULL
Reasons are described here: http://stackoverflow.com/questions/1843451/why-does-null-null-evaluate-to-false-in-sql-server
Nothing is equal to NULL, not even NULL itself. (NULL = NULL is NULL). =NULL is rarly if ever what you want.
Use IS NULL to check against NULL.
To add to existing answers, it depends whether you have ANSI_NULLS on or not, when using "= NULL".
-- This will print TRUE
SET ANSI_NULLS OFF;
IF NULL = NULL
PRINT 'TRUE'
ELSE
PRINT 'FALSE'
-- This will print FALSE
SET ANSI_NULLS ON;
IF NULL = NULL
PRINT 'TRUE'
ELSE
PRINT 'FALSE'
because of the three-valued-logic of SQL:
http://en.wikipedia.org/wiki/Null_%28SQL%29#Three-valued_logic_.283VL.29