数字金额转大写SQL函数实现

  1set ANSI_NULLS ON
  2set QUOTED_IDENTIFIER ON
  3go
  4
  5ALTER FUNCTION [dbo].[FN_CONTRACT_MONEY_UPPER] (@n_LowerMoney numeric(15,2),@v_TransType int)  
  6RETURNS VARCHAR(200AS  
  7BEGIN 
  8Declare @v_LowerStr VARCHAR(200-- 小写金额 
  9Declare @v_UpperPart VARCHAR(200
 10Declare @v_UpperStr VARCHAR(200-- 大写金额
 11Declare @i_I int
 12
 13set @v_LowerStr = LTRIM(RTRIM(ROUND(@n_LowerMoney,-2))) --四舍五入为指定的精度并删除数据左右空格--精确到百位
 14set @i_I = 1
 15set @v_UpperStr = ''
 16
 17while ( @i_I <= len(@v_LowerStr))
 18begin
 19      select @v_UpperPart = case substring(@v_LowerStr,len(@v_LowerStr- @i_I + 1,1)
 20                            WHEN  '.' THEN  ''
 21                            WHEN  '0' THEN  ''
 22                            WHEN  '1' THEN  ''
 23                            WHEN  '2' THEN  ''
 24                            WHEN  '3' THEN  ''
 25                            WHEN  '4' THEN  ''
 26                            WHEN  '5' THEN  ''
 27                            WHEN  '6' THEN  ''
 28                            WHEN  '7' THEN  ''
 29                            WHEN  '8' THEN  ''
 30                            WHEN  '9' THEN  ''
 31                            END
 32                          + 
 33                            case @i_I
 34                            WHEN  1  THEN  ''
 35                            WHEN  2  THEN  ''
 36                            WHEN  3  THEN  ''
 37                            WHEN  4  THEN  ''
 38                            WHEN  5  THEN  ''
 39                            WHEN  6  THEN  ''
 40                            WHEN  7  THEN  ''
 41                            WHEN  8  THEN  ''
 42                            WHEN  9  THEN  ''
 43                            WHEN  10  THEN  ''
 44                            WHEN  11  THEN  ''
 45                            WHEN  12  THEN  '亿'
 46                            WHEN  13  THEN  ''
 47                            WHEN  14  THEN  ''
 48                            WHEN  15  THEN  ''
 49                            WHEN  16  THEN  ''
 50                            ELSE ''
 51                            END
 52set @v_UpperStr = @v_UpperPart + @v_UpperStr
 53set @i_I = @i_I + 1
 54end
 55
 56if ( 0 = @v_TransType)
 57begin
 58set @v_UpperStr = REPLACE(@v_UpperStr,'零拾',''
 59set @v_UpperStr = REPLACE(@v_UpperStr,'零佰',''
 60set @v_UpperStr = REPLACE(@v_UpperStr,'零仟',''
 61set @v_UpperStr = REPLACE(@v_UpperStr,'零零零','')
 62set @v_UpperStr = REPLACE(@v_UpperStr,'零零','')
 63set @v_UpperStr = REPLACE(@v_UpperStr,'零角零分','')
 64set @v_UpperStr = REPLACE(@v_UpperStr,'零分','')
 65set @v_UpperStr = REPLACE(@v_UpperStr,'零角','')
 66set @v_UpperStr = REPLACE(@v_UpperStr,'零亿零万零元','亿元')
 67set @v_UpperStr = REPLACE(@v_UpperStr,'亿零万零元','亿元')
 68set @v_UpperStr = REPLACE(@v_UpperStr,'零亿零万','亿')
 69set @v_UpperStr = REPLACE(@v_UpperStr,'零万零元','万元')
 70set @v_UpperStr = REPLACE(@v_UpperStr,'万零元','万元')
 71set @v_UpperStr = REPLACE(@v_UpperStr,'零亿','亿')
 72set @v_UpperStr = REPLACE(@v_UpperStr,'零万','')
 73set @v_UpperStr = REPLACE(@v_UpperStr,'零元','')
 74set @v_UpperStr = REPLACE(@v_UpperStr,'零零','')
 75end
 76
 77-- 对壹元以下的金额的处理 
 78if ( '' = substring(@v_UpperStr,1,1))
 79begin
 80     set @v_UpperStr = substring(@v_UpperStr,2,(len(@v_UpperStr- 1))
 81end
 82
 83if ( '' = substring(@v_UpperStr,1,1))
 84begin
 85     set @v_UpperStr = substring(@v_UpperStr,2,(len(@v_UpperStr- 1))
 86end
 87
 88if ( '' = substring(@v_UpperStr,1,1))
 89begin
 90     set @v_UpperStr = substring(@v_UpperStr,2,(len(@v_UpperStr- 1))
 91end
 92
 93if ( '' = substring(@v_UpperStr,1,1))
 94begin
 95     set @v_UpperStr = substring(@v_UpperStr,2,(len(@v_UpperStr- 1))
 96end
 97
 98if ('' = substring(@v_UpperStr,1,1))
 99begin
100     set @v_UpperStr = '零元整'
101end
102
103return replace(replace(replace(replace(replace(replace(@v_UpperStr,'零角',''),'零分',''),'零元',''),'零拾',''),'零佰',''),'零仟','')+'圆整'
104END
posted @ 2007-09-30 13:09  中尉  阅读(1163)  评论(0编辑  收藏  举报