当前位置: 代码迷 >> Sql Server >> 小弟我想在SQLserver中实现一个函数,能够将10进制转化为36进制
  详细解决方案

小弟我想在SQLserver中实现一个函数,能够将10进制转化为36进制

热度:9   发布时间:2016-04-27 15:09:37.0
我想在SQLserver中实现一个函数,能够将10进制转化为36进制
0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ,能不能实现,特此请教。
比如     135   对应的为3R

------解决方案--------------------
--写成了两个函数,看起来清晰点

--一位转换函数
create function fn_36 (
@i int
)
returns varchar(1)
as
begin
return (case when @i <10 then cast(@i as varchar)
else char(ascii( 'A ')[email protected]) end)
end
go


--转换函数
create function fn_10to36 (
@i int
)
returns varchar(30)
as
begin

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

declare @m int
declare @s int
set @[email protected]
while @s> 36
begin
set @[email protected] % 36
set @[email protected]+dbo.fn_36(@m)
set @[email protected]/36
end
if @s> 0
set @[email protected]+dbo.fn_36(@s)
return @r
end

go

--调用测试
select dbo.fn_10to36(135)
--结果
------------------------------
R3

(1 行受影响)

select dbo.fn_10to36(65535)
--结果
------------------------------
FKE1

(1 行受影响)

  相关解决方案