分類: SQL函數(shù)分享系列 --創(chuàng)建函數(shù) 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
--測(cè)試示例 select [dbo].[hex](1808) as Hex
--運(yùn)行結(jié)果 /* Hex ---------- 710 */
--第二版 /**************************** 整數(shù)轉(zhuǎn)換成進(jìn)制 作者:不得閑 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
--測(cè)試示例 select dbo.IntToHex(1808)
--運(yùn)行結(jié)果 /* 710 */ |
|