DECLARE @n int;
SELECT @n = 21;
-- Decode as columns...
SELECT POWER( 2, 0) & @n AS Val1,
POWER( 2, 1) & @n AS Val2,
POWER( 2, 2) & @n AS Val3,
POWER( 2, 3) & @n AS Val4,
POWER( 2, 4) & @n AS Val5
/*
Val1 Val2 Val3 Val4 Val5
----------- ----------- ----------- ----------- -----------
1 0 4 0 16
*/
-- Decode as rows...
SELECT p2 AS Val
FROM (
SELECT POWER( 2, 0 ) AS p2 UNION ALL
SELECT POWER( 2, 1 ) UNION ALL
SELECT POWER( 2, 2 ) UNION ALL
SELECT POWER( 2, 3 ) UNION ALL
SELECT POWER( 2, 4 )
) AS p2
JOIN ( SELECT @n AS Num ) AS n
ON NULLIF( p2 & n.Num , p2 ) IS NULL
/*
Val
-----------
1
4
16
*/
This page was last updated on May 01, 2006 04:28 PM.