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

Detecting if bit set in numeric value

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

Problem

I have a tinyint column that represents a set of flags. Assuming I wanted to know if a specific bit was set or not, how can I do bitwise AND/OR in a stored procedure?

Solution

From your previous questions you use SQL Server. So you can use the & operator.

e.g. to see if the bit for 4 is on (and assuming NULL should return NULL)

SELECT CASE number & 4 WHEN 4 THEN 1 WHEN 0 THEN 0 END

Code Snippets

SELECT CASE number & 4 WHEN 4 THEN 1 WHEN 0 THEN 0 END

Context

StackExchange Database Administrators Q#25265, answer score: 5

Revisions (0)

No revisions yet.