10进制与36进制的转换函数

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go





-- =============================================
-- AUTHOR:		<ZHYD>
-- CREATE DATE: <2010-03-12>
-- DESCRIPTION:	<十进制转换成36进制>
-- =============================================

ALTER function   [dbo].[fn_10to36]   ( 
	@i   int 
) 
returns   varchar(30) 
as 
begin 

	declare   @r   varchar(30) 
	set   @r= '' 

	declare   @m   int 
	declare   @s   int 
	set   @s=@i 
	while   @s> =36 
	begin 
		set   @m=@s   %   36 
		set   @r=case   when   @m <10   then   cast(@m   as   varchar)   
			else   cast(char(ascii( 'A')+@m-10)   as   varchar)   end+@r 
		set   @s=@s/36 
	end 
		if   @s> 0   or   (@s=0   and   @r= '') 
			set   @r=case   when   @s <10   then   cast(@s   as   varchar)   
		else   cast(char(ascii( 'A')+@s-10)   as   varchar)   end+@r 
	return   @r 
end 



set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go




-- =============================================
-- AUTHOR:		<ZHYD>
-- CREATE DATE: <2010-03-12>
-- DESCRIPTION:	<36进制转换成10进制>
-- =============================================

ALTER function   [dbo].[fn_36to10]   ( 
@str   varchar(30) 
) 
returns   int
as 
begin 

declare @returnValue int
select @returnValue = 0
declare @str36 varchar(32)
declare @subWork varchar(1)
declare @workIndex int
select @str36 = '123456789ABCDEFGHIJKLMNOPQRSTUVWSYZ'

declare @len int
declare @i int
select @i = 1
select @len = datalength(@str)

while (@i <= @len)
begin
	select @subWork = SUBSTRING(@str, @i, 1) 
	select @workIndex = charindex( @subWork, @str36)
	select @returnValue = @returnValue + (@workIndex * power(36, @len-@i))
	select @i = @i + 1
end
	
return   @returnValue 
end 

你可能感兴趣的:(sql,Go)