I've been working with tsql for quite a while now but I've never seen binary anding or oring in WHERE clause. Now I'm developing new application that would benefit from applying a bitmask. Lets say I have 16 product grades. Each grade is representrd by a bit position in a bit[] column. So grade A109 would be 0000000000000001, grade B704 would be 0000001000000000, grade V64 is 0100000000000000 and so on. Any grade can only have single 1 in its array column. Now let's say I can turn each of those 3 grades into one another in manufacturing process. So my bit mask for this search would be 0100001000000001. How would I write a WHERE clause to list items of all those 3 grades?
I did some more research and the best solution is to compare masks with bitwise AND operator like this
WHERE mask1 & mask2 <> 0
This is easy, simple and cohesive.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With