Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

comparing bit array to bit mask in tsql

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?

like image 563
ArtK Avatar asked May 16 '26 21:05

ArtK


1 Answers

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.

like image 157
ArtK Avatar answered May 18 '26 14:05

ArtK