Technical Article

Java's String.hashCode() function in T-SQL

,

Pass the function a string, it returns an integer hash of the string. If you don't need to match Java, use the T-SQL binary_checksum() function, which will be much faster. (By the way, it looks like binary_checksum() uses a very similar algorithm: for all caps string, I get very similar results if instead of the "@h*31" in the code above I use "@h*16", and instead of "%4294967296" use "%4294967295". There must be something else too...).

Example:

select dbo.UTIL_JAVA_HASHCODE('abC1234567');

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[UTIL_JAVA_HASHCODE]'))
    drop function dbo.UTIL_JAVA_HASHCODE
GO

create function dbo.UTIL_JAVA_HASHCODE(@str varchar(max))
  returns int
  as
-------------------------------------------------------------------------------
-- Proc: UTIL_JAVA_HASHCODE
-- Desc: Replicate Java's String.HashCode() function
-- Inputs: @str: String
-- Outputs: Java hashcode of the string (4 byte integer)
-------------------------------------------------------------------------------
begin
declare @i int, @dl int, @h bigint
select @i = 1, @dl = len(@str), @h = 0
while @i <= @dl begin
    set @h = (@h*31 + ascii(substring(@str, @i, 1)))%4294967296
    set @i = @i + 1
end
if @h >= 2147483648 set @h = @h - 4294967296
return convert(int, @h)
end;
go

Rate

5 (2)

You rated this post out of 5. Change rating

Share

Share

Rate

5 (2)

You rated this post out of 5. Change rating