SqlServer金额转换函数,原创

fontain 2004-03-21 02:32:22

CREATE function dbo.Ntoc(@ChangeMoney decimal(20,2)) returns char(100)
--小写金额转大写函数
--参照方小庆(inrm@263.net)先生相应的delphi函数写成
--本函数作者:fontain(fontain@163.com)
as
begin
declare @CastChinese char(100)--将返回的结果
declare @C varchar(100)--相应位置汉字常量
declare @S char(100)--欲转换值的字符格式值
declare @L int--欲转换值的长度
declare @Z bit--布尔值,0假1真

declare @st char(100)--存放转换后的整数部分
declare @st1 char(100)--存放转换后的小数部分

declare @i int
declare @j int
declare @n int

select @st=''
select @C='零壹贰叁肆伍陆柒捌玖◇分角圆拾佰仟万拾佰仟亿拾佰仟万'

select @S=LTrim(RTrim(convert(char(50),@ChangeMoney)))
select @L=len(@S)
select @Z=Case When @ChangeMoney<1 then 1 else 0 end
--整数部分转换
select @i=1
while @i<=@L-3 begin
select @n=cast(substring(@S,@L-@i-2,1) as int)
select @st=Case when (@n=0) And (@Z=1 or (@i=9) or (@i=5)) then '' else substring(@C,@n+1,1) end +
Case when (@n=0) And ((@i<>9) And(@i<>5) And (@i<>1) or (@Z=1) And (@i=1)) then '' else substring(@C,(@i+13),1) end+
@st
select @Z=case when @n=0 then 1 else 0 end
select @i=@i+1
end
select @st=replace(@st,'亿万','亿')
select @st=replace(@st,'零圆','圆')
--转换完毕
--小数部分转换
select @n=cast(substring(@S,@L,1) as int)
select @st1=case when @n=0 then ''
else substring(@C,@n+1,1)+'分'
end
select @n=cast(substring(@S,@L-1,1) as int)
select @st1=case when @st1='' then
case when @n=0 then '整'
else substring(@C,@n+1,1)+'角整'
end
else
case when @n=0 then @st1
else substring(@C,@n+1,1)+'角'+@st1
end
end
--转换完毕
select @CastChinese=case when @ChangeMoney=0 then '零元整' else rtrim(@st)+rtrim(@st1) end
return @CastChinese
end


Win2000 adv server+sql server2000 企业版调试通过
...全文
308 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
wea1978 2004-03-22
  • 打赏
  • 举报
回复
收藏先。。。
pingfzp 2004-03-21
  • 打赏
  • 举报
回复
谢了~
RUKYO 2004-03-21
  • 打赏
  • 举报
回复
顶啊!
zjcxc 2004-03-21
  • 打赏
  • 举报
回复
以前已经有一个,可以实现将数字转换成金额大写或数字大写
dlpseeyou 2004-03-21
  • 打赏
  • 举报
回复
先收藏!!!
fontain 2004-03-21
  • 打赏
  • 举报
回复
怎么没人来啊,我以为大家都需要呢:(
progress99 2004-03-21
  • 打赏
  • 举报
回复
帮你顶!
j9988 2004-03-21
  • 打赏
  • 举报
回复
先收了,多谢! ̄
leeboyan 2004-03-21
  • 打赏
  • 举报
回复
先收了,慢慢看!

22,209

社区成员

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

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