SQL: Splitting a Bitmask into separate columns
Today I needed to split an integer (representing a bitmask) into its components, each component in a separate column. The following code from http://www.sqlservercentral.com/Forums/Topic1101943-392-1.aspx did the trick.
SELECT N, SIGN(N & 1) AS Bit1, SIGN(N & 2) AS Bit2, SIGN(N & 4) AS Bit3, SIGN(N & 8) AS Bit4, SIGN(N & 16) AS Bit5, SIGN(N & 32) AS Bit6, SIGN(N & 64) AS Bit7, SIGN(N & 128) AS Bit8 FROM ( SELECT 511 ) TestData(N) |