问select一个bigint,如果转换16进制字符输出?
表table,有一个字段bigint a,我想用select语句把字段a用一串16进制字符串输出,怎么怎么写?谢谢。
[解决办法]
create function inttohex(@i bigint)
returns varchar(15)
begin
declare @r varchar(15)
set @r=''
while @i/16>0
begin
set @r=
(case
when (@i % 16)<=9 then convert(varchar(1),@i % 16)
when (@i % 16)=10 then 'A'
when (@i % 16)=11 then 'B'
when (@i % 16)=12 then 'C'
when (@i % 16)=13 then 'D'
when (@i % 16)=14 then 'E'
when (@i % 16)=15 then 'F'
end) +@r
set @i=@i/16
end
if @i>0
set @r=(case
when (@i % 16)<=9 then convert(varchar(1),@i % 16)
when (@i % 16)=10 then 'A'
when (@i % 16)=11 then 'B'
when (@i % 16)=12 then 'C'
when (@i % 16)=13 then 'D'
when (@i % 16)=14 then 'E'
when (@i % 16)=15 then 'F'
end)+ @r
return @r
end
go
DECLARE @a BIGINT=123456789
SELECT stuff(master.dbo.fn_varbintohexstr(cast(@a as varbinary(6))),1,2,'')