SQL大小写金额转换

   --功能:  用于将小写的数值翻译成大写的字符串(支持到分,即小数点后两位)     
--入口参数:@decNum------数字型变量  
  
--返回:字符串  
--举例:select dbo.fn_ChnMoney(623.88)  
--    结果为“陆佰贰拾叁元捌角捌分”   
  
  
CREATE  FUNCTION fn_ChnMoney(@decNum decimal(18,2))  
RETURNS varchar(200)  
AS  
BEGIN  
  
 DECLARE @chvNum varchar(200)  
 DECLARE @chvMoney varchar(200)  
 DECLARE @chvTemp varchar(200)  
 DECLARE @intIntLen int  
 DECLARE @intI int  
 DECLARE @chvTempI varchar(200)  
 DECLARE @chvReturn varchar(200)  
  
  
 IF @decNum=0   
  SET @chvMoney = ''  
        Else  
 BEGIN  
   
  SET @chvTemp=convert(varchar(200),Round(@decNum*100,0))  
  IF charindex('.',@chvTemp,1)>0  
   SET @chvNum=left(@chvTemp,charindex('.',@chvTemp,1)-1)  
  ELSE  
   SET @chvNum=@chvTemp  
    
    
  SET @intIntLen=len(@chvNum)  
    
  SET @chvMoney=''  
  Set @chvReturn = ''  
  SET @intI=1  
    
  WHILE @intI <= @intIntLen  
  BEGIN  
   SET @chvTempI = substring(@chvNum,@intIntLen-@intI+1,1)  
   SET @chvMoney = substring('零壹贰叁肆伍陆柒捌玖',convert(int,@chvTempI)+1,1) + substring('分角元拾佰仟万拾佰仟亿拾佰仟兆拾佰仟京拾佰仟',(@intI-1)+1,1)  
   SET @intI = @intI + 1     
     
   SET @chvReturn = @chvMoney + @chvReturn  
  END  
 END  
  
 SET @chvReturn=Replace(@chvReturn,'零仟','')  
 SET @chvReturn=Replace(@chvReturn,'零佰','')  
 SET @chvReturn=Replace(@chvReturn,'零拾','')  
  
 while charindex('零零',@chvReturn,1)>0  
  SET @chvReturn=Replace(@chvReturn,'零零','')  
   
   
   
 SET @chvReturn=Replace(@chvReturn,'零兆','')  
 SET @chvReturn=Replace(@chvReturn,'零亿','亿')  
 SET @chvReturn=Replace(@chvReturn,'零万','')  
 SET @chvReturn=Replace(@chvReturn,'零元','')  
 SET @chvReturn=Replace(@chvReturn,'零角零分','')  
 SET @chvReturn=Replace(@chvReturn,'零角','')  
 SET @chvReturn=Replace(@chvReturn,'零分','')  
 SET @chvReturn=LTRIM(RTRIM(@chvReturn))  
   
  
 RETURN @chvReturn  
  
END

 

posted @ 2016-08-05 15:08  飘....  阅读(6033)  评论(1编辑  收藏  举报