Using vertical bar in SQL

I keep using (alien) code like this

Case When (variable & (2|4|8|16)>0) Then ... WHEN (variable & (1|32)>0 Then ... ... End 

I understand what is happening here, checking to see if there is 1 or 0 in 2 ^ 1, 2 ^ 2, 2 ^ 3, or 2 ^ 4 places of the variable variable . It is right? In any case, I still do not understand why this expression is written as it is. I cannot find documentation for this logic mainly because I don’t know what to actually name it.

+4
source share
1 answer

You are right, pipe symbol | matches the bitwise OR operator, while the ampersand & matches the bitwise AND operator (at least in some databases).

They check bits using these bitwise operators. The most likely reason they do it the way they did is because of "improved readability." For instance. easier to see which bits are checked when writing

 2|4|8|16 -- rather than 30 1|32 -- rather than 33 
+3
source

All Articles