使用SQL创建把数字转换成大写金额

Posted roychenyi

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用SQL创建把数字转换成大写金额相关的知识,希望对你有一定的参考价值。

SQL Server 把数字转换成大写金额,之前在网上找了一个,但是其中没有超过1000元的时候,第一位会有显示一个“仟”,SQL 已经优化,但感觉好像还可以优化,请诸位再优化一下吧。谢谢。
Create FUNCTION [dbo].[CapitalRMB]
(
 @Cost float
)
RETURNS varchar(50)
AS
----大写钱数----
BEGIN
 declare @returnStr varchar(50)
 if (@Cost >= 1000000000)
    Begin
  set @returnStr = '##########'
        return @returnStr
    end
 set @returnStr = '00000000000'
 set @returnStr = right(@returnStr + convert(varchar,convert(decimal(11,2),round(@cost,2))),12)
 set @returnStr = replace(@returnStr,'0','零')
 set @returnStr = replace(@returnStr,'1','壹')
 set @returnStr = replace(@returnStr,'2','贰')
 set @returnStr = replace(@returnStr,'3','叁')
 set @returnStr = replace(@returnStr,'4','肆')
 set @returnStr = replace(@returnStr,'5','伍')
 set @returnStr = replace(@returnStr,'6','陆')
 set @returnStr = replace(@returnStr,'7','柒')
 set @returnStr = replace(@returnStr,'8','捌')
 set @returnStr = replace(@returnStr,'9','玖')
           
 set @returnStr = @returnStr + '分'
 set @returnStr =  stuff(@returnStr,len(@returnStr)-1,0,'角')
 set @returnStr =  replace(@returnStr,'.','元')
 set @returnStr =  stuff(@returnStr,len(@returnStr)-5,0,'拾')
 set @returnStr =  stuff(@returnStr,len(@returnStr)-7,0,'佰')
 set @returnStr =  stuff(@returnStr,len(@returnStr)-9,0,'仟')
 set @returnStr =  stuff(@returnStr,len(@returnStr)-11,0,'万')
 set @returnStr =  stuff(@returnStr,len(@returnStr)-13,0,'拾')
 set @returnStr =  stuff(@returnStr,len(@returnStr)-15,0,'佰')
 set @returnStr =  stuff(@returnStr,len(@returnStr)-17,0,'仟')
 set @returnStr =  stuff(@returnStr,len(@returnStr)-19,0,'亿')
 
 set @returnStr =  replace(@returnStr,'零亿','零')
 set @returnStr =  replace(@returnStr,'零仟','零')
 set @returnStr =  replace(@returnStr,'零佰','零')
 set @returnStr =  replace(@returnStr,'零拾','零')
 set @returnStr =  replace(@returnStr,'零零零','零')
 set @returnStr =  replace(@returnStr,'零零','零')
 set @returnStr =  replace(@returnStr,'零万','万')
 set @returnStr =  replace(@returnStr,'零角','零')
 set @returnStr =  replace(@returnStr,'零分','')
 
 while left(@returnStr,1) = '零'
    Begin
    set @returnStr =  right(@returnStr,len(@returnStr)-1)
    end
 if ((left(@returnStr,1)='万')) 
 begin
  set @returnStr =  right(@returnStr,len(@returnStr)-1)
 end
  while left(@returnStr,1) = '零'
    Begin
    set @returnStr =  right(@returnStr,len(@returnStr)-1)
    end
 if ((left(@returnStr,1)='仟')) 
 begin
  set @returnStr =  right(@returnStr,len(@returnStr)-1)
 end
  while left(@returnStr,1) = '零'
    Begin
    set @returnStr =  right(@returnStr,len(@returnStr)-1)
    end
 if ((left(@returnStr,1)='佰')) 
 begin
  set @returnStr =  right(@returnStr,len(@returnStr)-1)
 end
   while left(@returnStr,1) = '零'
    Begin
    set @returnStr =  right(@returnStr,len(@returnStr)-1)
    end
 if ((left(@returnStr,1)='拾')) 
 begin
  set @returnStr =  right(@returnStr,len(@returnStr)-1)
 end
    while left(@returnStr,1) = '零'
    Begin
    set @returnStr =  right(@returnStr,len(@returnStr)-1)
    end
 if ((left(@returnStr,1)='元')) 
 begin
  set @returnStr =  right(@returnStr,len(@returnStr)-1)
 end
 while left(@returnStr,1) = '零'
    Begin
    set @returnStr =  right(@returnStr,len(@returnStr)-1)
    end
 while right(@returnStr,1) = '零'
    Begin
    set @returnStr =  left(@returnStr,len(@returnStr)-1)
    end
 
 set @returnStr =  replace(@returnStr,'亿万','亿零')
 set @returnStr =  replace(@returnStr,'零仟佰','仟零')
 set @returnStr =  replace(@returnStr,'仟佰','仟零')
 set @returnStr =  replace(@returnStr,'万仟','万零')
 set @returnStr =  replace(@returnStr,'亿仟','亿零')
 set @returnStr =  replace(@returnStr,'零零元','元')
 set @returnStr =  replace(@returnStr,'零元','元')
 set @returnStr =  replace(@returnStr,'零零零','零')
 set @returnStr =  replace(@returnStr,'零零','零')
 
 if (@returnStr='') 
 begin
  set @returnStr =  '零元'
 end
 if ((right(@returnStr,1)='元')) 
 begin
  set @returnStr =  @returnStr + '整'
 end
 return @returnStr
END

以上是关于使用SQL创建把数字转换成大写金额的主要内容,如果未能解决你的问题,请参考以下文章

js怎么把数字金额转换成中文大写金额

js怎么把数字金额转换为大写金额

如何自动把数字转换成大写的金钱?

转一个财务方面常用到的数字金额转成汉字大写金额 php类

小写数字自动转换成大写人民币金额两张表怎么在桌面找不同吗?

在WORD中怎样用VBA将人民币大写金额转换成小写金额