Thursday, June 11, 2009

Find the Binary value of a number (<1024)

SELECT e.val,
nullif(sign(bitand(e.val, power(2, 0))), 0) b0,
nullif(sign(bitand(e.val, power(2, 1))), 0) b1,
nullif(sign(bitand(e.val, power(2, 2))), 0) b2,
nullif(sign(bitand(e.val, power(2, 3))), 0) b3,
nullif(sign(bitand(e.val, power(2, 4))), 0) b4,
nullif(sign(bitand(e.val, power(2, 5))), 0) b5,
nullif(sign(bitand(e.val, power(2, 6))), 0) b6,
nullif(sign(bitand(e.val, power(2, 7))), 0) b7,
nullif(sign(bitand(e.val, power(2, 8))), 0) b8
FROM (SELECT 55 val
FROM dual) e;

No comments: