HiveBrain v1.2.0
Get Started
← Back to all entries
gotchasqlMinor

What's the difference between equality (=) and inequality (<>) on BIT field in SQL Server?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
fieldtheequalitybitwhatserversqldifferencebetweenand

Problem

Is there difference between WHERE [BitField] = 1 and WHERE [BitField] <> 0?

They seem logically equivalent.

SQL Server, however, disagrees and creates slightly different execution plans. (Which is bad because the second form doesn't use existing indexes very effectively.)

Am I missing something, or it is just the way inequality operators behave?

Solution

They seem logically equivalent.

They are. Even with ANSI_NULLS OFF "a SELECT statement that uses WHERE column_name <> XYZ_value returns all rows that are not XYZ_value and that are not NULL."

I found an old feedback item for this here, you can go vote for it.

Context

StackExchange Database Administrators Q#289942, answer score: 6

Revisions (0)

No revisions yet.