首页 > 数据库 >SQL Server中文大写金额转化函数

SQL Server中文大写金额转化函数

时间:2024-02-22 15:00:44浏览次数:33  
标签:set end 大写 replace stuff len SQL returnStr Server

SQL Server 中文金额 、大写金额:

CREATE FUNCTION fn_GetChineseCost
(
  @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,'零角','零')
     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
     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,'零零','零')

     if (@returnStr='') 
     begin
      set @returnStr =  '零元'
     end
     if ((right(@returnStr,1)='元')) 
     begin
      set @returnStr =  @returnStr + '整'
     end
     return @returnStr
END

select dbo.fn_GetChineseCost(987654321.23) as 金额 

 

标签:set,end,大写,replace,stuff,len,SQL,returnStr,Server
From: https://www.cnblogs.com/lgx5/p/18027366

相关文章

  • docker安装部署mysql8以及创建数据库
    mysql安装dockerpullmysql:8.0#创建mysql相关目录mkdir-p/server/data/docker/mysql/master/datadirdockerrun--namemysql-p53306:3306--restart=always--privileged=true\-v/server/data/docker/mysql/master/datadir:/var/lib/mysql\-v/s......
  • MySQL 数据库
    Linux下MySQL的安装与使用安装前说明查看是否安装过MySQL如果你是用rpm安装,检查一下RPMPACKAGE:rpm-qa|grep-imysql#-i忽略大小写检查mysqlservice:systemctlstatusmysqld.serviceMySQL的卸载1.关闭mysql服务systemctlstopmysqld.service2.查看当......
  • 为免费Hyper-V Server 2019打造本地图形管理界面
    Hyper-VServer是微软发布的免费虚拟化引擎,支持文本界面。本文通过在Hyper-VServer上本地安装chrome和windowsadmincenter,实现Hyper-VServer的本地图形管理界面。1、介绍Hyper-V是Microsoft的硬件虚拟化产品。它用于创建并运行计算机的软件版本,称为“虚拟机”。每......
  • Linux 安装Mysql
     首先通过 xshell 或者putty远程进入Linux命令行操作界面。【1】查看是否已经安装Mysqlrpm-qa|grepmysql如果你查看出来有东西,可以使用下面命令将其删除(xxx为文件全名)rpm-exxx【2】下载官方Mysql包wget-i-chttp://dev.mysql.com/get/mysql57-co......
  • MySQL字段按照特定规则排序
    分享一下数据库字段排序需求不是按照自然的顺序进行升序排序;需求:工单状态,按照生产中>挂起>待生产>已完成进行排序。解决方法:1、工单状态枚举对应可以设置为:生产中:10 ,挂起:20: ,待生产:30,已完成:40数据库where条件后面可以设置为:orderbyorder_statusasc。这种方法是最......
  • KEPServerEX与数据库通讯选型大全
    数据库作为重要的基础软件,是很多软件运行的根基,在信息化时代的今天也是必不可少的基石。KEPServerEX针对不同的数据库有不同的连接方案。关系型数据库 1.DataLogger插件将OPC服务器的数据存储到任何兼容ODBC的关系型数据库支持灵活的设置触发条件,控制何时存储支持自动创......
  • sql server配置主从同步环境下的日志收缩
    两种方法:通过图形界面逐步操作:(1)打开数据库属性窗口。(2)更改数据库恢复模式,将其从“完整”改成“简单”。(3)收缩数据库日志,选择“任务”->“收缩”。(4)收缩完成后,将数据库的恢复模式修改回“完整”.使用命令:(1)将数据库的恢复模式设置为“简单”:SQLALT......
  • 分析kube-apiserver单次创建namespace耗时
    日志输出#业务日志I022022:12:39.14936440965multi_config_multi_clientset.go:63]begintowaitcachesyncI022022:12:39.25046140965multi_config_multi_clientset.go:67]waitcachesyncendI022022:12:39.25644040965multi_config_multi_clientset.go:......
  • MySQL-15.存储过程和函数
    C-15.存储过程和函数MySQL从5.0版本开始支持存储过程和函数。存储过程和函数能够将复杂的SQL逻辑封装在一起,应用程序无须关注存储过程和函数内部复杂的SQL逻辑,而只需要简单地调用存储过程和函数即可。1.存储过程概述1.1理解含义:存储过程的英文是StoredProcedure。它的思想......
  • Sqlmap
    Sqlmapwindows下环境配置将sqlmap与python安装路径添加到PATH环境变量将.PY;添加到PATHEXT环境变量打开cmd输入sqlmap-h验证是否成功对于GET型注入bp抓包,找到请求参数加入到url中sqlmap-u""--batch--dbssqlmap-u""--batch-D数据库名--tablessqlmap-u""......