Sql Server十六进制转换为十进制

CREATE  function HexToInt (@hex varchar(100))
returns int
as
begin
    Declare @result int,@iPos int,@iTmp int,@iLoop int,@tmp varchar(16)
    Set @tmp = '0123456789ABCDEF'
    Select @result = 0,@iPos = 0
    While @iPos <   Len(@hex)
    Begin
          Set  @iTmp = 
               CharIndex(substring(@hex,Len(@hex) - @iPos,1),@tmp)-1
          Set @iLoop = 1
          While @iLoop <= @iPos and @iTmp > 0
          begin
              Set @iTmp = @iTmp * 16
              Set @iLoop = @iLoop + 1
          end
          Set @result = @result + @iTmp
          Set @iLoop = @iLoop + 1     
          Set @iPos = @iPos + 1
    End

    return @result
End

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、付费专栏及课程。

余额充值