if OBJECTPROPERTY( OBJECT_ID( 'dbo.BytesToBits' ) , 'IsTableFunction' ) is not null
        drop function dbo.BytesToBits
GO
create function dbo.BytesToBits (
    @Bytes binary(8),
    @Start tinyint = 1,
    @End tinyint = 64
)
returns table
as
    /********************************************************************************/
    /* Created By       : Umachandar Jayachandran (UC)                                                  */
    /* Created On       : 28 July 2002                                                  */
    /* Description      : This function converts binary data to bit sequence.           */
    /********************************************************************************/
    /*  Resources  :    https://umachandar.com/resources.htm                         */
    /********************************************************************************/
    return (
        -- Counts from left to right!
        -- Numbered from 1..64!
        select b3.Num, b3.Val
          from (
            select ( ( b1.Num - 1 )* 8 ) + ( 7 - b2.Num ) + 1,
                   cast( sign( b1.Byte & power( 2 , b2.Num ) ) as bit )
              from (
                    select b.Num, substring( @Bytes, b.Num, 1 )
                      from (
                            select 1 union all select 2 union all select 3 union all select 4 union all select 5
                             union all select 6 union all select 7 union all select 8
                          ) as b( Num )
                 ) as b1( Num, Byte )
             cross join (
                    select b.Num
                      from (
                            select 0 union all select 1 union all select 2 union all select 3 union all select 4
                             union all select 5 union all select 6 union all select 7
                          ) as b( Num )
                     where b.Num Between 0 And 7
                 ) as b2
             ) as b3( Num, Val )
         where b3.Num Between @Start And @End
    )
go

select * from dbo.BytesToBits( 0xF3E10DF44669B33E, default, default ) order by num
/*
Num         Val  
----------- ---- 
          1    1 
          2    1 
          3    1 
          4    1 
          5    0 
          6    0 
          7    1 
          8    1 
*/
This page was last updated on May 01, 2006 04:28 PM.