Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Check for a set bit in an Integer value

Author  Topic 

CXXXV
Starting Member

12 Posts

Posted - 2009-08-28 : 13:33:52
I am storing binary value as an Integer in a table. I need to check if a particular bit is set. (6 bit number)

So for instance: the value is 15 which means that the fourth bit is set to 1.

I can't figure out the where clause for that?

Help?

rohitkumar
Constraint Violating Yak Guru

472 Posts

Posted - 2009-08-28 : 13:42:42
declare @myint int
set @myint = 15

SELECT (case when @myint & 1 = 1 then 1 else 0 end) AS '1st'
, (case when @myint & 2 = 2 then 1 else 0 end) AS '2nd'
, (case when @myint & 4 = 4 then 1 else 0 end) AS '3rd'
, (case when @myint & 8 = 8 then 1 else 0 end) AS '4th'
, (case when @myint & 16 = 16 then 1 else 0 end) AS '5th'
, (case when @myint & 32 = 32 then 1 else 0 end) AS '6th'
Go to Top of Page
   

- Advertisement -