﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / Article Discussions / Article Discussions by Author / Discuss content posted by Mike Arney  / Java's String.hashCode() function in T-SQL / Latest Posts</title><generator>InstantForum.NET v4.1.4</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Sat, 20 Mar 2010 04:20:17 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Java's String.hashCode() function in T-SQL</title><link>http://www.sqlservercentral.com/Forums/Topic516137-1315-1.aspx</link><description>Thanks for this enhancement!  I have always been reluctant to use the "select @var=@var + col from tbl" construct, for fear of it stopping working in some future release.  But it seems like it's here to stay.  And with that kind of performance improvement it's hard to resist.</description><pubDate>Wed, 28 Jan 2009 09:05:21 GMT</pubDate><dc:creator>Mike Arney</dc:creator></item><item><title>RE: Java's String.hashCode() function in T-SQL</title><link>http://www.sqlservercentral.com/Forums/Topic516137-1315-1.aspx</link><description>Thanks for a very useful function.  When applied to a scalar value, the routine is fine.  However, if you're applying it to thousands of rows, it's very slow because of the while loop.Here's an alternative.  [code]alter 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)-------------------------------------------------------------------------------begindeclare @h bigintset @h = 0select @h = (@h*31 + ascii(substring(@str,X.pos,1)))%4294967296   from (select top(len(@str))              row_number() over (order by getdate()) as pos            from sys.all_objects) as Xif @h &amp;gt;= 2147483648 set @h = @h - 4294967296return convert(int, @h)end;go[/code]With the following logic:[quote]set statistics time ongodeclare @table table ( hashCode int )insert into @table    select global.dbo.UTIL_JAVA_HASHCODE( X.varchar32field )from TableWith33000Rows as X go[/quote]Using the original function, the insert completed in 53s (avg of 3 runs). Applying the original function to a varchar(64) field, the elapsed time grew to 100s.After revising the function to eliminate the while loop, the varchar(32) processing completes in 11s and the varchar(64) processing in only 14s.  (If you have a tally table feel free to use it instead of the derived table from sys.all_objects.):)</description><pubDate>Wed, 28 Jan 2009 08:58:31 GMT</pubDate><dc:creator>antonio.collins</dc:creator></item><item><title>Java's String.hashCode() function in T-SQL</title><link>http://www.sqlservercentral.com/Forums/Topic516137-1315-1.aspx</link><description>Comments posted to this topic are about the item [B]&lt;A HREF="/scripts/T-SQL/63409/"&gt;Java's String.hashCode() function in T-SQL&lt;/A&gt;[/B]</description><pubDate>Thu, 12 Jun 2008 11:40:12 GMT</pubDate><dc:creator>Mike Arney</dc:creator></item></channel></rss>