{"id":801,"date":"2016-01-08T16:06:55","date_gmt":"2016-01-08T21:06:55","guid":{"rendered":"http:\/\/www.chiplynch.com\/wordpress\/?p=801"},"modified":"2016-01-08T16:08:50","modified_gmt":"2016-01-08T21:08:50","slug":"base-conversion-in-sql","status":"publish","type":"post","link":"https:\/\/www.chiplynch.com\/wordpress\/?p=801","title":{"rendered":"Base conversion in SQL"},"content":{"rendered":"<p>Here&#8217;s something silly I threw together that no one will probably use, which makes it perfect for posting here.  We ran across software that has a table that has an Id that ALSO has a unique char(4) field that, as far as we can tell, serves no rational purpose.  But the values need to be unique and alphanumeric, apparently with no special characters at all.  The Id field is a positive integer that starts at 1, so it seemed like a fine opportunity to convert to base 62, using 0-9,A-Z, and a-z as the 62 candidate numerals.<\/p>\n<p>We ended up not doing this, since there was more complicated slower and more difficult to comprehend code available, but I still think this is useful:<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">ALTER FUNCTION &#x5B;dbo].&#x5B;Base62Convert] ( @InputInt BIGINT ) RETURNS CHAR(4)\r\nAS\r\n-- Input: Positive Integer\r\nBEGIN\r\n    DECLARE @Result CHAR(4) = ''\r\n\r\n    IF (@InputInt &lt; 0)\r\n        return cast('No negative numbers allowed.' as int);\r\n\r\n\tIF (@InputInt &gt;= POWER(62,4))\r\n\t\treturn cast('Number too big, going home.' as int);\r\n\r\n    WHILE (@InputInt &gt; 0)\r\n    BEGIN\r\n        SET @Result = CHAR(@InputInt % 62 + \r\n\t\tCASE WHEN @InputInt % 62 &lt; 10 THEN 48\r\n\t\t\tWHEN @InputInt %62 &lt; 36 then 55 \r\n\t\t\tELSE 61 END)\r\n\t\t+ @Result\r\n\tSET @InputInt = FLOOR(@InputInt\/62)\r\n    END\r\n\r\n    RETURN RIGHT('0000' + @Result, 4)\r\nEND\r\n\r\nGO\r\n\r\n-- Test:\r\nselect dbo.base62Convert(power(62,4)-1), power(62,4)-1\r\n<\/pre>\n<p>Which will give you nice unique 4-character identifiers up to 14,776,335.<\/p>\n<p>Enjoy!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s something silly I threw together that no one will probably use, which makes it perfect for posting here. We ran across software that has a table that has an Id that ALSO has a unique char(4) field&#8230; <a class=\"read-more\" href=\"https:\/\/www.chiplynch.com\/wordpress\/?p=801\">Read More<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[29,8,9],"tags":[],"class_list":["post-801","post","type-post","status-publish","format-standard","hentry","category-data","category-software","category-work"],"_links":{"self":[{"href":"https:\/\/www.chiplynch.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/801","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.chiplynch.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.chiplynch.com\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.chiplynch.com\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.chiplynch.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=801"}],"version-history":[{"count":3,"href":"https:\/\/www.chiplynch.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/801\/revisions"}],"predecessor-version":[{"id":804,"href":"https:\/\/www.chiplynch.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/801\/revisions\/804"}],"wp:attachment":[{"href":"https:\/\/www.chiplynch.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=801"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.chiplynch.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=801"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.chiplynch.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=801"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}