22,300
社区成员




--十进制 转换为 十六进制
create procedure sp_gethexfromnumeric
@stringid nvarchar(8) output,
@id numeric
as
declare @hex_string nchar(20)
declare @hex_char char(1)
declare @pos int
declare @tempid numeric
declare @divisor int
set @hex_string = '0123456789abcdef'
set @stringid = ''
set @tempid = @id
---set @divisor = 4096 --support in 8 char
set @divisor = 268435456
---if @id > 65536
if @id > 4294967296
set @stringid = 'ffffffff'
else
begin
while (@divisor>0)
begin
set @pos = @tempid/@divisor
if(@pos>15)
set @pos =15
set @hex_char = substring(@hex_string,@pos+1,1)
set @stringid = @stringid+@hex_char
set @tempid = @tempid - @pos*@divisor
set @divisor = @divisor/16
end
end
go
declare @result nvarchar(8)
declare @id numeric
set @id = 1500
exec sp_gethexfromnumeric @result output,@id
select @result
create function ntoc(@i int)
returns char(1)
as
begin
declare @r char(1)
if @i<10
set @r=ltrim(@i)
else
set @r=char(@i+55)
return @r
end
go
select dbo.ntoc(a)+dbo.ntoc(b)+dbo.ntoc(c)+dbo.ntoc(d)+dbo.ntoc(e) from(
select number/36/36/36/36%36 a,number/36/36/36%36 b,number/36/36%36 c,number/36%36 d,number%36 e from master..spt_values where type='p'
)t
/*
-----
00000
00001
00002
00003
00004
00005
00006
00007
00008
00009
0000A
0000B
0000C
0000D
0000E
0000F
0000G
0000H
0000I
0000J
0000K
0000L
0000M
0000N
0000O
0000P
0000Q
0000R
0000S
0000T
0000U
0000V
0000W
0000X
0000Y
0000Z
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
0001A
0001B
0001C
0001D
0001E
.....
001KQ
001KR
001KS
001KT
001KU
001KV
(2048 行受影响)
*/
go
drop function ntoc
--自己写个十进制转36进制???