I had an issue today where I tried to import some old quotes from a different database into our InforCRM Saleslogix system.
I found this post and I had to fiddle with it to get it to work. Excuse the awful SQL, but this get's the values:
What I needed to unpack was the BASE36 value of each digit. 0-9 is easy, but A = 10, B = 11, etc.
After that it's a matter of multiplying the base 36 value by the power of it's place starting at 0. So the first digit is it's (base36 value) * 36^0. The second digit is its (base 36 value) * 36^1, etc.
Add them all together to get the Pretty Key. ALTERNATEKEYSUFFIX
SELECT A.DIGIT1*POWER(36,0) + A.DIGIT2*POWER(36,1) + A.DIGIT3*POWER(36,2) + A.DIGIT4*POWER(36,3) + A.DIGIT5*POWER(36,4) + A.DIGIT6*POWER(36,5) AS PRETTYKEY FROM ( SELECT QUOTEID , ALTERNATEKEYSUFFIX , CASE WHEN SUBSTRING(QUOTEID, LEN(QUOTEID)-0,1) LIKE '[0-9]' THEN SUBSTRING(QUOTEID, LEN(QUOTEID)-0,1) ELSE ASCII(SUBSTRING(QUOTEID, LEN(QUOTEID)-0,1)) - ASCII('A')+10 END AS DIGIT1 , CASE WHEN SUBSTRING(QUOTEID, LEN(QUOTEID)-1,1) LIKE '[0-9]' THEN SUBSTRING(QUOTEID, LEN(QUOTEID)-1,1) ELSE ASCII(SUBSTRING(QUOTEID, LEN(QUOTEID)-1,1)) - ASCII('A')+10 END AS DIGIT2
, CASE WHEN SUBSTRING(QUOTEID, LEN(QUOTEID)-2,1) LIKE '[0-9]' THEN SUBSTRING(QUOTEID, LEN(QUOTEID)-2,1) ELSE ASCII(SUBSTRING(QUOTEID, LEN(QUOTEID)-2,1)) - ASCII('A')+10 END AS DIGIT3
, CASE WHEN SUBSTRING(QUOTEID, LEN(QUOTEID)-3,1) LIKE '[0-9]' THEN SUBSTRING(QUOTEID, LEN(QUOTEID)-3,1) ELSE ASCII(SUBSTRING(QUOTEID, LEN(QUOTEID)-3,1)) - ASCII('A')+10 END AS DIGIT4
, CASE WHEN SUBSTRING(QUOTEID, LEN(QUOTEID)-4,1) LIKE '[0-9]' THEN SUBSTRING(QUOTEID, LEN(QUOTEID)-4,1) ELSE ASCII(SUBSTRING(QUOTEID, LEN(QUOTEID)-4,1)) - ASCII('A')+10 END AS DIGIT5
, CASE WHEN SUBSTRING(QUOTEID, LEN(QUOTEID)-5,1) LIKE '[0-9]' THEN SUBSTRING(QUOTEID, LEN(QUOTEID)-5,1) ELSE ASCII(SUBSTRING(QUOTEID, LEN(QUOTEID)-5,1)) - ASCII('A')+10 END AS DIGIT6 FROM SalesLogix.sysdba.QUOTE
) A
Here is some C# code. It got a little fiddly with data-types but this got what I needed for the ALTERNATESUFFIX
string inforId = "Q06VXA0087HW"; double prettyKey = 0; for (int i = 1; i <= 6; i = i + 1) { char c = Convert.ToChar(inforId.Substring(inforId.Length - i, 1)); int base36 = 0; if (Char.IsNumber(c)) { Int32.TryParse(c.ToString(), out base36); } else { base36 = ((int)c-55); } prettyKey += base36 * Math.Pow(36, i - 1); } Console.WriteLine(prettyKey);
|