|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: 2 days ago @ 3:16 PM
Points: 2,749,
Visits: 1,405
|
|
|
|
|
|
Old Hand
      
Group: General Forum Members
Last Login: Thursday, August 27, 2009 12:56 AM
Points: 348,
Visits: 7
|
|
Very cool! Extremely useful article about architecture to use bit operations! In my case, I could use this to simulate bitmap indexes. Regards, Geert
|
|
|
|
|
Old Hand
      
Group: General Forum Members
Last Login: Tuesday, March 29, 2005 2:31 PM
Points: 393,
Visits: 1
|
|
I love bits... when you understand the bits, then all else falls into place. Also makes me think back to the good ol' M/L days on the 6502, 8088, Z80 . Thanks David for writing about the bits. In SQL Server there are 2 usefull bit functions supplied by MS in the Master DB and available in any current database. ( I do not like their "all lowercase" names ) Select fn_replinttobitstring(65535) -- 32 bits of passed integer and select fn_replbitstringtoint('0000000000000011111111111111111') -- integer of passed bit string
Once you understand the BITs, all the pieces come together
|
|
|
|
|
Old Hand
      
Group: General Forum Members
Last Login: Thursday, August 27, 2009 12:56 AM
Points: 348,
Visits: 7
|
|
What??  Now THIS is good news, I'm building an ETL process here in which I'll process flags with the bitmap technique described in the article. Damn, I'd be building these functions myself, I have never found any docs about these, not even in Microsoft Press books. Thanks for mentioning the functions, ThomasH ! Regards, Geert
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: 2 days ago @ 3:16 PM
Points: 2,749,
Visits: 1,405
|
|
Pause a moment whilst I swear profusely  I wish I had known about fn_replinttobitstring & fn_replbitstringtoint, it would have made my life so much easier! I'll add them to my little black book of undocumented functions! Thanks
LinkedIn Profile
|
|
|
|
|
Old Hand
      
Group: General Forum Members
Last Login: Tuesday, March 29, 2005 2:31 PM
Points: 393,
Visits: 1
|
|
I wrote a function that expands to 64 bits (8x8) that might be usefull... Create Function fn_BigIntToBinary (@bi BigInt) RETURNS Varchar(72) AS Begin /* -- Returns 64 bits of passed BigInt, each byte separated by a space. Select dbo.fn_BigIntToBinary(123456789) */ Declare @RetVal Varchar(72), @Hi16a BigInt, @Hi16b Bigint, @Low16a BigInt, @Low16b BigInt, @Hi32 BigInt, @RetVal1 VarChar(72) Select @Hi32 = @bi / Power(Convert(BigInt,2), 32) select @Hi16a = (@bi / (65536 * 256)) & (Power(2, 16) - 1), @Hi16b = (@bi / 65536) & (Power(2, 16) - 1), @Low16a = (@bi / 256) & (Power(2, 16) - 1), @Low16b = @bi & (Power(2, 16) - 1), @RetVal1 = case When @bi > Power(Convert(BigInt,2), 32) then dbo.fn_BigIntToBinary(@Hi32) Else '00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000' End Set @RetVal = Right(@RetVal1, 35) + ' ' + Right(fn_replinttobitstring(@Hi16a), 8) + ' ' + Right(fn_replinttobitstring(@Hi16b), 8) + ' ' + Right(fn_replinttobitstring(@Low16a), 8) + ' ' + Right(fn_replinttobitstring(@Low16b), 8) Return @RetVal End Hope it cut&pastes ok....
Once you understand the BITs, all the pieces come together
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Yesterday @ 6:25 AM
Points: 6,861,
Visits: 8,045
|
|
|
|
|
|
Old Hand
      
Group: General Forum Members
Last Login: Thursday, August 27, 2009 12:56 AM
Points: 348,
Visits: 7
|
|
RTFM. Use test conditions described in article "Using bits to Store Data".
|
|
|
|
|
Old Hand
      
Group: General Forum Members
Last Login: Thursday, August 27, 2009 12:56 AM
Points: 348,
Visits: 7
|
|
Another approach: Let's check for Friday, which is bit 7, I presume (I have no clue, check out in the Books online) Bitmap is: 0000 0010 Integer value is: 2 AND the bitmap field with this integer value Field & 2 Result is 2 If field is NOT friday, result will be zero. Thus: CASE WHEN Field|1=1 THEN 'Saturday' WHEN field|2=2 THEN 'Friday' .... END But still I recommend you to read the article, because a bitmap may contain different "1" bits. The CASE statement will see the first one and ignore the rest.
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Yesterday @ 6:25 AM
Points: 6,861,
Visits: 8,045
|
|
|
|
|