[MSSQL]将十进制转成十六进制

--创建函数 create function [dbo].[hex](@cardno int ) returns varchar (100) as begin declare @temp_mod int declare @i int declare @result varchar(100) declare @temp_x int declare @result_values int set @result='' set @i=1 set @temp_x=0 while @cardno>0 begin set @temp_mod=@cardno%16 set @cardno=@cardno/16 set @result=(case @temp_mod when 10 then 'A' when 11 then 'B' when 12 then 'C' when 13 then 'D' when 14 then 'E' when 15 then 'F' else ltrim(str(@temp_mod)) end )+@result end return @result end --测试示例 select [dbo].[hex](1808) as Hex --运行结果 /* Hex ---------- 710 */ --第二版 /**************************** 整数转换成进制 作者:不得闲 QQ: 75492895 Email: appleak46@yahoo.com.cn ****************************/ go Create Function IntToHex(@IntNum int) returns varchar(16) as begin declare @Mods int,@res varchar(16) set @res='' while @IntNum <> 0 begin set @Mods =@IntNum % 16 if @Mods > 9 set @res = Char(Ascii('A')+@Mods-10)+@res else set @res = Cast(@Mods as varchar(4)) + @res set @IntNum = @IntNum/16 end return @res end --测试示例 select dbo.IntToHex(1808) --运行结果 /* 710 */
select number16,substring(number16,1,1) a,substring(number16,2,1) b, substring(number16,3,1) c, substring(number16,4,1) d, substring(number16,5,1) e,substring(number16,6,1) f, substring(number16,7,1) g, substring(number16,8,1) h, convert(decimal(10,0),isnull(replace(replace(replace(replace(replace(replace(substring(number16,1,1),'A','10'),'B','11'),'C','12'),'D','13'),'E','14'),'F','15'),0)) * power(16,7) + convert(decimal(10,0),isnull(replace(replace(replace(replace(replace(replace(substring(number16,2,1),'A','10'),'B','11'),'C','12'),'D','13'),'E','14'),'F','15'),0)) * power(16,6) + convert(decimal(10,0),isnull(replace(replace(replace(replace(replace(replace(substring(number16,3,1),'A','10'),'B','11'),'C','12'),'D','13'),'E','14'),'F','15'),0)) * power(16,5) + convert(decimal(10,0),isnull(replace(replace(replace(replace(replace(replace(substring(number16,4,1),'A','10'),'B','11'),'C','12'),'D','13'),'E','14'),'F','15'),0)) * power(16,4) + convert(decimal(10,0),isnull(replace(replace(replace(replace(replace(replace(substring(number16,5,1),'A','10'),'B','11'),'C','12'),'D','13'),'E','14'),'F','15'),0)) * power(16,3) + convert(decimal(10,0),isnull(replace(replace(replace(replace(replace(replace(substring(number16,6,1),'A','10'),'B','11'),'C','12'),'D','13'),'E','14'),'F','15'),0)) * power(16,2) + convert(decimal(10,0),isnull(replace(replace(replace(replace(replace(replace(substring(number16,7,1),'A','10'),'B','11'),'C','12'),'D','13'),'E','14'),'F','15'),0)) * power(16,1) + convert(decimal(10,0),isnull(replace(replace(replace(replace(replace(replace(substring(number16,8,1),'A','10'),'B','11'),'C','12'),'D','13'),'E','14'),'F','15'),0)) * power(16,0) from test_16to10 where len(number16)>6 -- create table test_16to10(number16 varchar(10) not null); --ex: number16 value is:'D95B7D3C'
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值