您的位置:首页 > 数据库

SQL小写金额换大写金额

2008-03-22 17:50 549 查看
/*转换小写数字为大写数字*/

declare @l varchar(100) -- 小写金额
declare @u1 varchar(100)
declare @u varchar(100)-- 大写金额
declare @i int
set @l='123456000.789'
set @i=1
set @l = ltrim(rtrim(str(@l,20,2))) --保留两位小数并删除数据左右空格

select @u = '' --置初值
while ( @i <= len(@l))--大于字符串长度即退出
begin
set @u1=
case substring(@l,len(@l) - @i + 1,1)
--从字符串最后一位倒序取字符
when '.' then '圆'
when '0' then '零'
when '1' then '壹'
when '2' then '贰'
when '3' then '叁'
when '4' then '肆'
when '5' then '伍'
when '6' then '陆'
when '7' then '柒'
when '8' then '捌'
when '9' then '玖'
end
+
case @i

when 1 then '分'
when 2 then '角'
when 3 then ''
when 4 then ''
when 5 then '拾'
when 6 then '佰'
when 7 then '仟'
when 8 then '万'
when 9 then '拾'
when 10 then '佰'
when 11 then '仟'
when 12 then '亿'
when 13 then '拾'
when 14 then '佰'
else ''
end
set @u = @u1+@u
--连接上次循环的字符串,从右向左倒序连接
set @i = @i+1
--递增,下次循环取上一字符
end
select @u = replace(@u,'零圆','零')
select @u = replace(@u,'零拾','零')
select @u = replace(@u,'零佰','零')
select @u = replace(@u,'零仟','零')
select @u = replace(@u,'零零零','零')
select @u = replace(@u,'零零','零')
select @u = replace(@u,'零角零分','整')
select @u = replace(@u,'零分','整')
select @u = replace(@u,'零角','零')
select @u = replace(@u,'零亿零万零元','亿元')
select @u = replace(@u,'亿零万零元','亿元')
select @u = replace(@u,'零亿零万','亿')
select @u = replace(@u,'零万零元','万元')
select @u = replace(@u,'万零元','万元')
select @u = replace(@u,'零亿','亿')
select @u = replace(@u,'零万','万')
select @u = replace(@u,'零元','元')
select @u = replace(@u,'零零','零')

print @u
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: