关于ASCLL转十进制问题

jie361539146 2014-05-28 03:24:24
用存储过程如何实现从表中取值出来,然后通过while循环依次取两位数转换为10进制?
例如:
@info取值结果为字符串:’414231323334353637383920’
首先,先通过函数substring截取2位字符,’41’,然后把41转换为10进制,
得65。最后通过SQL指令select char(65)转换后,对应结果为 ’A’。
然后,再截取2位字符,’42’, 然后把41转换为10进制,得65。最后通
过SQL指令select char(65)转换后,对应结果为 ’B’。
如此类推,直至全部转换后,把字符串加在一起,可得条码号 ’AB123456789’。
...全文
146 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
改改表查询语句就可以了
  • 打赏
  • 举报
回复

create table ascll(a varchar(10),b varchar(10))
insert ascll
select '30','48' union all 
select '31','49' union all 
select '32','50' union all 
select '33','51' union all 
select '34','52' union all 
select '35','53' union all 
select '36','54' union all 
select '37','55' union all 
select '38','56' union all 
select '39','57' union all 
select '41','65' union all 
select '42','66'

create proc test
	@info nvarchar(100)
as
begin
	declare @value nvarchar(100)
	declare @int int
	set @value=''
	set @int=1
	while @int<(LEN(@info)-1)
	begin
		set @value=@value+char((select TOP 1 b from ascll where a=substring(@info,@int,2)))
		set @int=@int+2
	end
	select @value
end
/*
exec test 
@info='414231323334353637383920'


AB123456789
*/

jie361539146 2014-05-28
  • 打赏
  • 举报
回复
引用 9 楼 chwnrthd 的回复:
while @int<(LEN(@info)-2) 在while循环后面 @value单独加 @value=@value+char(20)
执行存储过程,输入参数时,出现报错消息 248,级别 16,状态 1,过程 test,第 9 行 转换 nvarchar 值 '414231323334353637383920' 时溢出了整数列。
  • 打赏
  • 举报
回复
while @int<(LEN(@info)-2) 在while循环后面 @value单独加 @value=@value+char(20)
  • 打赏
  • 举报
回复
while @int<(LEN(@info)-2)
jie361539146 2014-05-28
  • 打赏
  • 举报
回复
引用 5 楼 chwnrthd 的回复:
少了65转A部分 稍等~

最后面的20是不去转换它的。
  • 打赏
  • 举报
回复

create proc test
	@info nvarchar(100)
as
begin
	declare @value nvarchar(100)
	declare @int int
	set @value=''
	set @int=0
	while @int<LEN(@info)
	begin
		set @value=@value+char((select b from ascll where a=substring(@info,@int,2)))
		set @int=@int+2
	end
	select @value
end

  • 打赏
  • 举报
回复
少了65转A部分 稍等~
  • 打赏
  • 举报
回复

create table ascll(a varchar(10),b varchar(10))
如 a =41 b=65  

create proc test
	@info nvarchar(100)
as
begin
	declare @value nvarchar(100)
	declare @int int
	set @value=''
	set @int=0
	while @int<LEN(@info)
	begin
		set @value=@value+(select b from ascll where a=substring(@info,@int,2))
		set @int=@int+2
	end
	select @value
end

差不多就这样了 自己改改
jie361539146 2014-05-28
  • 打赏
  • 举报
回复
0 48 1 49 2 50 . . . . . . . . 65 我是这样子简单计算的,17比1多16,所以49+16=65。ASCLL码表里面有详细的转码表的
  • 打赏
  • 举报
回复
‘41’转换为10进制,得65 怎么得的写出来 不清楚怎么转的
jie361539146 2014-05-28
  • 打赏
  • 举报
回复
SqlServer 存储过程怎么写呢?

22,209

社区成员

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

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