SQL 数字金额转为英文形式

摘录自 http://blog.sina.com.cn/s/blog_43ad83920100nkfp.html

  1 IF EXISTS ( SELECT  *
  2             FROM    SYS.OBJECTS
  3             WHERE   NAME LIKE ‘F_NUM_ENG‘
  4                     AND TYPE = ‘FN‘ )
  5     DROP FUNCTION F_NUM_ENG
  6  GO
  7 CREATE FUNCTION [DBO].[F_NUM_ENG] ( @NUM NUMERIC(15, 2) )
  8 RETURNS VARCHAR(400)
  9 AS
 10     BEGIN
 11         DECLARE @I INT ,
 12             @HUNDREDS INT ,
 13             @TENTH INT ,
 14             @ONE INT
 15         DECLARE @THOUSAND INT ,
 16             @MILLION INT ,
 17             @BILLION INT
 18         DECLARE @NUMBERS VARCHAR(400) ,
 19             @S VARCHAR(15) ,
 20             @RESULT VARCHAR(400)
 21         SET @NUMBERS = ‘ONE       TWO       THREE     FOUR      FIVE      ‘
 22             + ‘SIX       SEVEN     EIGHT     NINE      TEN       ‘
 23             + ‘ELEVEN    TWELVE    THIRTEEN  FOURTEEN  FIFTEEN   ‘
 24             + ‘SIXTEEN   SEVENTEEN EIGHTEEN  NINETEEN  ‘
 25             + ‘TWENTY    THIRTY    FORTY     FIFTY     ‘
 26             + ‘SIXTY     SEVENTY   EIGHTY    NINETY    ‘
 27         SET @S = RIGHT(‘000000000000000‘ + CAST(@NUM AS VARCHAR(15)), 15)
 28         SET @BILLION = CAST(SUBSTRING(@S, 1, 3) AS INT)--将12位整数分成4段:十亿、百万、千、百十个
 29         SET @MILLION = CAST(SUBSTRING(@S, 4, 3) AS INT)
 30         SET @THOUSAND = CAST(SUBSTRING(@S, 7, 3) AS INT)
 31         SET @RESULT = ‘‘
 32         SET @I = 0
 33         WHILE @I <= 3
 34             BEGIN
 35                 SET @HUNDREDS = CAST(SUBSTRING(@S, @I * 3 + 1, 1) AS INT)--百位0-9
 36                 SET @TENTH = CAST(SUBSTRING(@S, @I * 3 + 2, 1) AS INT)
 37                 SET @ONE = ( CASE @TENTH
 38                                WHEN 1 THEN 10
 39                                ELSE 0
 40                              END ) + CAST(SUBSTRING(@S, @I * 3 + 3, 1) AS INT)--个位0-19
 41                 SET @TENTH = ( CASE WHEN @TENTH <= 1 THEN 0
 42                                     ELSE @TENTH
 43                                END )--十位0、2-9
 44                 IF ( @I = 1
 45                      AND @BILLION > 0
 46                      AND ( @MILLION > 0
 47                            OR @THOUSAND > 0
 48                            OR @HUNDREDS > 0
 49                          )
 50                    )
 51                     OR ( @I = 2
 52                          AND ( @BILLION > 0
 53                                OR @MILLION > 0
 54                              )
 55                          AND ( @THOUSAND > 0
 56                                OR @HUNDREDS > 0
 57                              )
 58                        )
 59                     OR ( @I = 3
 60                          AND ( @BILLION > 0
 61                                OR @MILLION > 0
 62                                OR @THOUSAND > 0
 63                              )
 64                          AND ( @HUNDREDS > 0 )
 65                        )
 66                     SET @RESULT = @RESULT + ‘ ‘--百位不是0则每段之间加连接符,
 67                 IF ( @I = 3
 68                      AND ( @BILLION > 0
 69                            OR @MILLION > 0
 70                            OR @THOUSAND > 0
 71                          )
 72                      AND ( @HUNDREDS = 0
 73                            AND ( @TENTH > 0
 74                                  OR @ONE > 0
 75                                )
 76                          )
 77                    )
 78                     SET @RESULT = @RESULT + ‘ ‘--百位是0则加连接符AND
 79                 IF @HUNDREDS > 0
 80                     SET @RESULT = @RESULT + RTRIM(SUBSTRING(@NUMBERS,
 81                                                             @HUNDREDS * 10 - 9,
 82                                                             10)) + ‘ HUNDRED‘
 83                 IF @TENTH >= 2
 84                     AND @TENTH <= 9
 85                     BEGIN
 86                         IF @HUNDREDS > 0
 87                             --SET @[email protected]+‘ AND ‘
 88                             SET @RESULT = @RESULT + ‘ ‘
 89                         SET @RESULT = @RESULT + RTRIM(SUBSTRING(@NUMBERS,
 90                                                               @TENTH * 10
 91                                                               + 171, 10))
 92                     END
 93                 IF @ONE >= 1
 94                     AND @ONE <= 19
 95                     BEGIN
 96                         IF @TENTH > 0
 97                             SET @RESULT = @RESULT + ‘-‘
 98                         ELSE
 99                             IF @HUNDREDS > 0
100                                 SET @RESULT = @RESULT + ‘  ‘
101                         SET @RESULT = @RESULT + RTRIM(SUBSTRING(@NUMBERS,
102                                                               @ONE * 10 - 9,
103                                                               10))
104                     END
105                 IF @I = 0
106                     AND @BILLION > 0
107                     SET @RESULT = @RESULT + ‘ BILLION‘
108                 IF @I = 1
109                     AND @MILLION > 0
110                     SET @RESULT = @RESULT + ‘ MILLION‘
111                 IF @I = 2
112                     AND @THOUSAND > 0
113                     SET @RESULT = @RESULT + ‘ THOUSAND‘
114                 SET @I = @I + 1
115             END
116         IF SUBSTRING(@S, 14, 2) <> ‘00‘
117             BEGIN
118                 SET @RESULT = @RESULT + ‘ AND ‘
119
120                 SET @RESULT = @RESULT
121                     + REPLACE([DBO].[F_NUM_ENG](CAST(SUBSTRING(@S, 14, 2) AS INT)),
122                               ‘ONLY‘, ‘ CENTS ‘)
123             END
124         SET @RESULT = UPPER(@RESULT) + ‘ ONLY‘
125         RETURN(@RESULT)
126     END
127 GO
128 SELECT [DBO].[F_NUM_ENG](804.00)
129 SELECT [DBO].[F_NUM_ENG](804.54)
时间: 2024-10-07 01:37:06

SQL 数字金额转为英文形式的相关文章

JS数字金额转为大写金额

/** 数字金额大写转换(可以处理整数,小数,负数) */function smalltoBIG(n){var fraction = ['角', '分'];var digit = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'];var unit = [ ['元', '万', '亿'], ['', '拾', '佰', '仟'] ];var head = n < 0? '欠': '';n = Math.abs(n); var s = ''; fo

Sql Server 数字金额转中文金额 函数

在做一些订单金额.合同金额转换的时候,经常会遇到数字转成中文的情况,所以整理了一个数字转中文的函数: /* 说明:数字金额转中文金额 示例:187.4 转成 壹佰捌拾柒圆肆角整 */ CREATE FUNCTION [dbo].[CNumeric](@num numeric(14,2)) returns nvarchar(100) BEGIN Declare @n_data nvarchar(20),@c_data nvarchar(100),@n_str nvarchar(10),@i int

PHP算法--将数字金额转换成大写金额

最近在看一些PHP算法题,遇到一个将数字金额转换成大写金额的小算法题,这里贴出自己的一个例子. 注:这个小算法适用于10万以内的金额. <?php //$num = 12345.67; function RMB_Upper($num) { $num = round($num,2); //取两位小数 $num = ''.$num; //转换成数字 $arr = explode('.',$num); $str_left = $arr[0]; // 12345 $str_right = $arr[1]

JS版 数字 金额 格式化 方法

/** * JS版 数字 金额格式化 * @param string s 需要处理的数字串 * @param string n 保留小数的位数 */function fmoney(s, n) { n = n >= 0 && n <= 20 ? n : 2; s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(n) + "";//更改这里n数也可确定要保留的

数字金额转汉字金额

早上公交上总想这个问题,终于写出来了,不知道有错误没 1 $chinese = array("零","一","二","三","四","五","六","七","八","九"); 2 $str = 140032002005; 3 $arr = array(); 4 5 $num = floor(strlen($s

通过xshell远程登录linux在vim编辑器里编辑东西按数字键出现英文字母的解决办法

在公司里面,大部分的人都是远程连接控制服务器,下面来解决一个新手使用xshell时出现的问题 有的人使用xshell远程登录linux服务器,然后在vi编辑器下编辑东西出现了按小键盘的数字键出现英文字母的情况,解决办法如下: 1. 选中文件中的属性选项. 2. 选择终端中的VT模式,在初始数字键盘模式下的三个选项中选择设置为普通即可.

ORA-06502: PL/SQL: 数字或值错误 : 字符串缓冲区太小解决办法

1.今天写的存储过程在执行过程中,报如下错误. exec PRO_T_008pro_update_add_delete(17,1,1,1,1,45.0,54.0,45.0,45.0,45.0,54.0,45.0,54.0,'生产厂家','CYB10-2',54.0,45.0,25.0,1.0,45.0,25.0,1.0,45.0,25.0,1.0,45.0,1,4545.0,0,0,0,'no'); begin PRO_T_008pro_update_add_delete(17,1,1,1,1,

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

系统里有牵扯到财务.合同等方面的处理时,常常需要把数字金额转成汉字大写金额(貌似这样正规),转一个转换的php class吧!<?php// 诸海加(ALPHA .z)// 2000-7-19 class ChineseNumber{//var $basical=array(0=>"零","一","二","三","四","五","六","七&qu

将数字字符串转为数字(java)

1 //讲数字字符串转为数值型(此处不考虑输入字符串的合法性),不允许使用parseInt方法和Character.digit 2 public class ConvertNumstr { 3 public static void main(String[] args) { 4 System.out.print(convertStr2Num("-2015")); 5 } 6 public static int convertStr2Num(String str) { 7 int len