Unix Technical Forum

BINARY_CHECKSUM algorithm

This is a discussion on BINARY_CHECKSUM algorithm within the SQL Server forums, part of the Microsoft SQL Server category; --> Hello, Do you know if the algorithm for the BINARY_CHECKSUM function in documented somewhere? I would like to use ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-29-2008, 08:10 AM
Orly Junior
 
Posts: n/a
Default BINARY_CHECKSUM algorithm

Hello,

Do you know if the algorithm for the BINARY_CHECKSUM function in documented
somewhere?
I would like to use it to avoid returning some string fields from the
server.
By returning only the checksum I could lookup the string in a hashtable and
I think this could make the code more efficient on slow connections.

Thanks in advanced and kind regards,

Orly Junior



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-29-2008, 08:10 AM
David Portas
 
Posts: n/a
Default Re: BINARY_CHECKSUM algorithm

I don't know where the algorithm is documented but I don't think it will
help you. There are many more possible strings than checksums so there isn't
a one-to-one correspondence between them. Unless your set of possible
strings is constrained to a small set you couldn't guarantee to translate a
checksum back into a string and you'd still have to maintain a lookup table
of strings on the client side. So if your set of strings is small and
constrained then you may as well invent your own codes. Alternatively, take
a look at Huffman Coding or one of the zip compression algorithms.

--
David Portas
SQL Server MVP
--


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-29-2008, 08:10 AM
Erland Sommarskog
 
Posts: n/a
Default Re: BINARY_CHECKSUM algorithm

Orly Junior (nomail@nomail.com) writes:
> Do you know if the algorithm for the BINARY_CHECKSUM function in
> documented somewhere? I would like to use it to avoid returning some
> string fields from the server. By returning only the checksum I could
> lookup the string in a hashtable and I think this could make the code
> more efficient on slow connections.


Risky business. The checksum algorithm is fairly simple-minded. I beleive
it uses some xor mechanism. I don't have the references around right now,
but I recall that SQL Server MVP Steve Kass demonstrated how some quite
small changes could result in the same checksum.

Better in such case, to augment the table with a timestamp column. Such
a column is automatically updated every time SQL Server updates the
row. So you could store the timestamp client side, and pass that value,
if the table has the same value, there is no need for a refresh.

--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-29-2008, 08:11 AM
Steve Kass
 
Posts: n/a
Default Re: BINARY_CHECKSUM algorithm

Orly,

Here is what I believe it does for a single varchar
column. It's not a particularly good hash function at
all.

create function binary_checksum_varchar (
@t varchar(1000)
) returns int as begin
declare @b bigint set @b = 0
declare @c tinyint
declare @s bit set @s = 0
declare @i int set @i = 1


while @i <= len(@t) begin
set @c = ascii(substring(@t,@i,1))
set @b = @b / 16 * 16 + @b % 16 ^ @c / 16
set @b = @b * 16 + @c % 16
if @c >= 128 begin
set @b = @b ^ 0xFF
set @s = 1 - @s
end
set @b = @b % 0x0100000000 ^ @b / 0x0100000000
set @i = @i + 1
end


if @s = 1 set @b = @b ^ 0xFFFFFFFF
if @b >= 0x80000000 set @b = @b | 0xFFFFFFFF00000000
return @b
end
go


You'll find more information if some of the threads
here:

http://groups.google.co.uk/groups?q=...kass+sqlserver

Steve Kass
Drew University

Orly Junior wrote:

> Hello,
>
> Do you know if the algorithm for the BINARY_CHECKSUM function in documented
> somewhere?
> I would like to use it to avoid returning some string fields from the
> server.
> By returning only the checksum I could lookup the string in a hashtable and
> I think this could make the code more efficient on slow connections.
>
> Thanks in advanced and kind regards,
>
> Orly Junior
>
>
>

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 02:13 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com