I am wondering if there is a short way (without programming the whole thing) of determining the largest number in a binary value in T-SQL.
In a column I store what days are selected (1 = sunday, 2 = monday, .. 7 = saturday) bitwise.
My translation table:
-- day of week
-- 1 == 1 -> POWER(2,0) -> sunday
-- 2 == 2 -> POWER(2,1) -> monday
-- 3 == 4 -> POWER(2,2)
-- 4 == 8 -> POWER(2,3)
-- 5 == 16 -> POWER(2,4)
-- 6 == 32 -> POWER(2,5)
-- 7 == 64 -> POWER(2,6) -> saturday
So if I select FOR EXAMPLE sunday and saturday my binary value is 65.
How can I select with T-SQL that 65 will be 7??
Edit
Lets say I have two record with a two columns:
ID | SelectedDays
------|--------------
1 | 65
2 | 3
So, ID 1 will return 7, because that will be the saturday that is selected selected and ID 2 will return monday.
This returns 1 to 7 depending of the date.
logaritmis the inverse ofpowerto obtanin logaritm in base 2, the trick is ‘divide by the base’. FLOOR, in its turn, provides the most significative bit.