Sql按数字加字母方式来进行增长

thecityofsky 2011-05-23 10:18:06
要求:
1.字符串长度为5位
2.增长方式不是按10进制或者16进制而是按0...9,A,B...Z(不包含I,O,Q)来进行增长,例如:00001...00009,0000A...0000Z,00010
3.数据量比较大,要求最小量的使用内存
...全文
318 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
打一壶酱油 2011-05-23
  • 打赏
  • 举报
回复

--十进制 转换为 十六进制

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
-晴天 2011-05-23
  • 打赏
  • 举报
回复
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
AcHerat 2011-05-23
  • 打赏
  • 举报
回复

--自己写个十进制转36进制???
thecityofsky 2011-05-23
  • 打赏
  • 举报
回复
自己顶一个啊,要求是不包含I,J,O,Q 有没有什么好的方便啊,用sql函数或者存储过程来处理,节省内存消耗,又减少网络流量

22,300

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧