MS SQL -1 and null


 Very confusing discovery. I have an int field , say A, that has -1, null and other numerals.

Where I run a query like below,

Select * from mytable where A <> -1 and B = 'Bob' , I get no rows returned. Are -1 and null the same in MS SQL 2012??

When I do either of below, I get the one row which has Bob
select * from mytable where B='Bob'
select * from mytable where (A is null or A <> -1) and B='Bob'

A   B
-1    Anna
null  Bob
1 Mary
2 Mo
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Brian CroweDatabase AdministratorCommented:
NULL has no value so it will not even be considered in a deterministic formula like "<> -1".  Do not confuse NULL with zero.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Scott PletcherSenior DBACommented:
It's best to get an actual understanding of this, as it will help tremendously in your future SQL.  The basic "rules" here are:
1) It's easiest to consider NULL as "unknown" for evaluation purposes.
2) For a row to be selected, the conditions in the WHERE clause must evaluate to True.  SQL never wants to "lie" to you and include a non-matched row in the result.

Now, let's look at your specific conditions:

where A <> -1 and B = 'Bob'

If A is NULL, the condition can be rephrased as:
is an <unknown value> not equal to -1?
Hmm, clearly the answer is also "unknown", since an unknown int value could be -1 or it could be 2billion - 1.  Likewise:
is an <unknown value> equal to, say, 5?
is also "unknown" for the same reason.  Walk through a few of these and you'll see that an "unknown" column always yield an "unknown" result and thus cannot be declared to be true.

Therefore, yes, if you want null As to be included you must do this:
 (A is null or A <> -1)
[Don't use ISNULL(), as any function on a column prevents index seeks.]

OK, test time, what would you expect to see from this query?:

select 1 where cast(null as int) in (select cast(null as int))
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.