惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

罗磊的独立博客
Cisco Talos Blog
Cisco Talos Blog
C
Check Point Blog
博客园_首页
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Martin Fowler
Martin Fowler
Recorded Future
Recorded Future
S
Security @ Cisco Blogs
L
LINUX DO - 最新话题
博客园 - 司徒正美
P
Privacy International News Feed
G
Google Developers Blog
I
Intezer
Hacker News - Newest:
Hacker News - Newest: "LLM"
博客园 - 聂微东
The GitHub Blog
The GitHub Blog
C
Cybersecurity and Infrastructure Security Agency CISA
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Scott Helme
Scott Helme
K
Kaspersky official blog
I
InfoQ
Y
Y Combinator Blog
T
The Blog of Author Tim Ferriss
Webroot Blog
Webroot Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
大猫的无限游戏
大猫的无限游戏
D
Docker
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
W
WeLiveSecurity
Microsoft Azure Blog
Microsoft Azure Blog
Spread Privacy
Spread Privacy
量子位
H
Hacker News: Front Page
Simon Willison's Weblog
Simon Willison's Weblog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
SecWiki News
SecWiki News
S
Security Affairs
Latest news
Latest news
人人都是产品经理
人人都是产品经理
C
CERT Recently Published Vulnerability Notes
S
Security Archives - TechRepublic
V
Visual Studio Blog
T
Troy Hunt's Blog
S
Secure Thoughts
F
Fortinet All Blogs
V
V2EX
The Register - Security
The Register - Security
J
Java Code Geeks
MongoDB | Blog
MongoDB | Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO

博客园 - ok_008

解决GitHub加载和下载慢问题 mysql命令行爱好者必备工具mycli What is a Back Order mysql中实现字符串分割sp_split Down Payment 和 Deposit的差异 Select逻辑顺序图 排名趋势公式 批量备份数据库脚本(PowerShell版) 批量替换存储过程内容脚本sp_SqlReplace 生成建表脚本up_CreateTable Data Model for Certificate Implementation of Message Receiver Data Model for Message Receiver SQL SERVER 批量生成编号 修改hosts文件 PHP-问题处理验证码无法显示出来 PHP-问题处理Fatal error: Uncaught Error: Call to undefined function mb_strlen() PHP-问题处理Fatal error: Uncaught Error: Call to undefined function simplexml_load_file() PHP-生成缩略图和添加水印图-学习笔记 SQLServer地址搜索性能优化例子
10进制转33进制
ok_008 · 2017-02-09 · via 博客园 - ok_008

1,开始

 昨天在CSDN上看到这一个提问帖子:《用SQL写一个存储过程10进制转33进制代码 》,原帖的问题是这样:

急求10进制转33进制代码。
 就是我想在SQL中创建一个存储过程“T10TO33”可以实现将10进制的数据转换成33进制。
33进制规则是:1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,J,K,L,M,N,P,Q,R,S,T,V,W,X,Y,Z,0
字母:I、O、U不用,其余正常流水,哪位大神帮忙写一个,谢谢

2.分析

不同进制数据间的转换,记得很久之前写过类似的,参加《X进制与10进制之间的转换》,基本的算法是相同的。

在这里的要求,进制转换中,26个字母中需排除I,O,U三个字母不用,那么原来的X进制与10进制之间转换方法中,就不能直接使用Char()函数。需要对用到的数字和字母重新作个排序,再定位转换。

  2.1.初始化一个表变量@tb_tmp,内容如下:

  

declare @tb_tmp as table(id int identity(0,1) primary key,radix char(1))
;with cte as (select top (36) row_number() over(order by getdate())-1 as id from sys.columns)
    insert into @tb_tmp(radix)
    select case when id>9 then char(55+id) else rtrim(id) end as radix from cte where char(55+id) not in ('I','O','U') 

  2.2 假设一个10进制的变量@input_int  int=50,循环对@input_int除33取模(@input_int%33),得到的模,拿到2.1中的@tb_tmp匹配id,定位到对应的基数radix.即对应到33进制数据位。把得到radix写入变量@output ( @output =  找到的radix + @output)。每次循环,@input_int=@input_int/33,实现10进制拆分。当@input_int=0时,退出循环,返回@output,作为10进制到33进制的结果值。

set @output=''
while(1=1)
begin   
    select @output=Convert(nvarchar(1024),Case (@input_int%33) when 0 then rtrim(@input_int%33) else (select radix from @tb_tmp where id=@input_int%33) end+@output),@input_int=@input_int/33
    if @input_int=0 break
end

3.完整代码:

if object_id('T10TO33') Is not null
    Drop Proc T10TO33
Go
create procedure T10TO33(
@input_int int,
@output nvarchar(1024) output
)
as
set nocount on
declare @tb_tmp as table(id int identity(0,1) primary key,radix char(1))
;with cte as (select top (36) row_number() over(order by getdate())-1 as id from sys.columns)
    insert into @tb_tmp(radix)
    select case when id>9 then char(55+id) else rtrim(id) end as radix from cte where char(55+id) not in ('I','O','U') 
set @output=''
while(1=1)
begin   
    select @output=Convert(nvarchar(1024),Case (@input_int%33) when 0 then rtrim(@input_int%33) else (select radix from @tb_tmp where id=@input_int%33) end+@output),@input_int=@input_int/33
    if @input_int=0 break
end
go

4.测试:

declare @reuslt nvarchar(1024)

exec T10TO33 0,@reuslt output
print @reuslt --0

exec T10TO33 7,@reuslt output
print @reuslt --7
 
exec T10TO33 32,@reuslt output
print @reuslt --Z
 
exec T10TO33 33,@reuslt output
print @reuslt --10
 
exec T10TO33 34,@reuslt output
print @reuslt --11

exec T10TO33 50,@reuslt output
print @reuslt --1H
 
exec T10TO33 99,@reuslt output
print @reuslt --30
 
exec T10TO33 100,@reuslt output
print @reuslt --31

(完)