Please see these two statements:
select 'true' where (1 = NULL) ;
select 'true' where not (1 = NULL) ;
Both (!) of them return empty!
So that means that the expressions (1=NULL)
and not (1=NULL)
both return false! But how can that be? I thought the operator NOT(expression)
will invert the result of the expression ?
According to Microsoft: "Comparing NULL to a non-NULL value always results in FALSE."
OK, but that would mean that the operator NOT
must invert the result. But why does it do this not then?
https://msdn.microsoft.com/en-us/library/ms188074.aspx
EDIT: I posted the wrong article. My quote above is from this article: https://msdn.microsoft.com/en-us/library/ms175118.aspx
It seems this article has an error, as stated below in the answers and comments.
So, T-SQL implements what is known as Three-Valued Logic. That means that each logical expression present in T-SQL code can evaluate to TRUE
, FALSE
OR NULL
. Now SQL Server gives you 2 options to handle logical expressions with NULLs
with the SET ANSI_NULL command. The default behaviour of SQL Server (SET ANSI_NULL ON) is that every logical comparison with NULL
will return NULL
. So the following expressions
NULL = NULL;
1 = NULL;
1 <> NULL;
will all evaluate to NULL
. If for some reason you want the logical expression to return true or false even if they have NULL
values in them, you have to turn ANSI_NULL OFF but it is not advisable.
Small edit: The only case that a logical expression including NULL
will evaluate to something other than NULL
is the following:
(NULL) OR (TRUE) = TRUE
So the following T-SQL code
SET ANSI_NULLS ON;
GO
IF ((NULL=NULL) OR (1=1))
PRINT 'True';
GO
will actually print True
.