Java 'Unhash' as T-SQL?

Mike Arney wrote a nice little hashcode function in T-SQL.

Could someone convert this java unhash code into T-SQL to complete the set...?

public static String unhash(int target) { StringBuilder unhashed = new StringBuilder(); if (target < 0) { // String with hash of Integer.MIN_VALUE, 0x80000000 unhashed.append("\u0915\u0009\u001e\u000c\u0002"); if (target == Integer.MIN_VALUE) return unhashed.toString(); // Find target without sign bit set target = target & Integer.MIN_VALUE; } unhash0(unhashed, target); return unhashed.toString(); }

private static void unhash0(StringBuilder partial, int target) { int div = target / 31; int rem = target % 31;

 if (div &lt;= Character.MAX_VALUE) {
     if (div != 0)
         partial.append((char) div);
     partial.append((char) rem);
 } else {
     unhash0(partial, div);
     partial.append((char) rem);


more ▼

asked Jun 11, 2010 at 09:27 AM in Default

avatar image

1 1 1 1

To recreate a hash system like this, you'd be better off using CLR and calling that from TSQL, rather than trying to force this through TSQL.

Jun 11, 2010 at 09:55 AM Grant Fritchey ♦♦

I'm with Grant. This could be done in SQL, but you are much better off writing something like this in C# and importing it as CLR.

Jun 11, 2010 at 01:37 PM TimothyAWiseman
(comments are locked)
10|1200 characters needed characters left

2 answers: sort voted first

I expect so .

more ▼

answered Jun 11, 2010 at 09:35 AM

avatar image

Fatherjack ♦♦
43.8k 79 102 118

Excellent! One of the best answers I've seen in a while.

Jun 11, 2010 at 09:55 AM Grant Fritchey ♦♦

Thanks Fatherjack, accurate but not very helpful!

Jun 11, 2010 at 10:26 AM JavaMan

@JavaMan, sorry, couldn't resist! Its a hot, tired Friday afternoon here. I think Grant has answered the question for you though, its not something suited to TSQL, much better using CLR

Jun 11, 2010 at 10:37 AM Fatherjack ♦♦
(comments are locked)
10|1200 characters needed characters left

I'm not a C programmer so I don't actually know what the original code does. If someone could come up with an explanation of what is being done by the code, I'll take a crack at it just because it might be fun.

more ▼

answered Jun 13, 2010 at 07:38 PM

avatar image

Jeff Moden
2.3k 3 7 13

(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.



asked: Jun 11, 2010 at 09:27 AM

Seen: 1701 times

Last Updated: Jun 11, 2010 at 09:27 AM

Copyright 2018 Redgate Software. Privacy Policy