可执行任意进制转换,2进制,8进制,32进制,36进制,进制数依赖于所能表示的字典数,如下'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ' 36个字母,最大是36进制,下面关键一句是SET @alldigits = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ',用取字符串位置的方式得到结果。
IF OBJECT_ID('dbo.DecToBase') IS NOT NULLDROP FUNCTION dbo.DecToBase;GO
CREATE FUNCTION dbo.DecToBase(@val BIGINT,@base INT)RETURNS VARCHAR(63)ASBEGIN
DECLARE @r AS VARCHAR(63),@alldigits AS VARCHAR(36)
SET @alldigits = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ' SET @r = ''
WHILE @val > 0 BEGIN
SET @r = SUBSTRING(@alldigits,@val%@base+1,1) + @r
SET @val = @val / @base END RETURN @rENDgo