SQL Server TSQL BIT =0 and < >1

ff29svar  于 2023-05-21  发布在  其他
关注(0)|答案(2)|浏览(127)

Are there any differences if I write

select * from table where bit_field = 0

or

select * from table where bit_field <> 1

?

UPDATE: I've got a report that it is better to use "bit_field = 0" version because if you have some indexes on a field an use the second option there is some issue with "Seek Predicates"... It has two seek predicates one with < 1 and other with > 1... or something. I don't have any example to show. :( After the change from <> to =, there is a decrease of exclusive locks (X) and intent exclusive locks (IX) Any thoughts on that?

pes8fvy9

pes8fvy91#

There should be no differences between the two queries. If there are no NULL values present, then obviously any value which is zero is also the same thing as not being one. If there areNULL s present, it doesn't change anything, because a NULL record would not be returned from either query.

See the demo below.

Demo

9rbhqvlz

9rbhqvlz2#

There should be no difference, because a bit only takes on the values of 0 and 1.

Both will exclude NULL values.

相关问题