Lets say you have the next code:
DECLARE @A INT = 1,
@B INT = NULL;
IF (@B != @A)
SELECT 1;
ELSE IF (@B = @A)
SELECT 2;
ELSE
SELECT 3;
As you can see variable @A
equals '1' for sure and variable @B
certainly doesn't. I'm executing that part of code in Microsoft SQL Server Management Studio 2014 and I'm getting '3' as result. That means @A
is not the same as @B
, but it's also not different than @B
. How is that even possible? What am I missing here?
NULL
values. Almost any comparison withNULL
returnsNULL
, which is treated as false. This is covered both in learning material on SQL, documentation, and in other Stack Overflow questions.NULL
values can be found in this awesome answer here.IS NULL
/IS NOT NULL
operators