TSQL 大写金额转换函数

  1. create function dbo.TCN  
  2. (@x varchar(50)) returns varchar(100)  
  3. begin  
  4.   declare @r varchar(100),@zs varchar(50),@xs varchar(50)  
  5.   declare @a int,@b int  
  6.    
  7.   if @x='' or len(@x)=0  
  8.   begin  
  9.     select @r=''  
  10.     return @r  
  11.   end  
  12.    
  13.   declare @gc table(i tinyint,d varchar(5))  
  14.   declare @hs table(i tinyint,d varchar(5))  
  15.      
  16.   insert into @gc(i,d)  
  17.    select 1,'壹' union all  
  18.    select 2,'贰' union all  
  19.    select 3,'叁' union all  
  20.    select 4,'肆' union all  
  21.    select 5,'伍' union all  
  22.    select 6,'陆' union all  
  23.    select 7,'柒' union all  
  24.    select 8,'捌' union all  
  25.    select 9,'玖'  
  26.       
  27.   insert into @hs(i,d)  
  28.    select 1,'' union all  
  29.    select 2,'拾' union all  
  30.    select 3,'佰' union all  
  31.    select 4,'仟' union all  
  32.    select 5,'万' union all  
  33.    select 6,'拾' union all  
  34.    select 7,'佰' union all  
  35.    select 8,'仟' union all  
  36.    select 9,'亿' union all  
  37.    select 10,'拾'  
  38.      
  39.   select @x=case when charindex('.',@x,1)=0 then @x+'.00'  
  40.                  when (len(@x)-charindex('.',@x,1))=1 then @x+'0'  
  41.                  else @x end  
  42.    
  43.   select @zs=left(@x,charindex('.',@x,1)-1),  
  44.          @xs=right(@x,len(@x)-charindex('.',@x,1)),  
  45.          @r='',  
  46.          @a=1,  
  47.          @b=len(@zs)  
  48.             
  49.   while(@a<=len(@zs))  
  50.   begin  
  51.     if substring(@zs,@a,1)='0' and right(@r,1)<>'零'  
  52.     begin  
  53.        select @r=@r+'零'  
  54.     end  
  55.     else  
  56.     begin  
  57.       If substring(@zs,@a,1)<>'0'  
  58.       begin  
  59.         select @r=@r+isnull((select top 1 d from @gc where i=cast(substring(@zs,@a,1) as tinyint)),'')  
  60.         select @r=@r+isnull((select top 1 d from @hs where i=cast(@b as tinyint)),'')  
  61.       end  
  62.     end  
  63.           
  64.     if len(@zs)>=6  
  65.     begin  
  66.       if left(right(@zs,5),1)='0' and @b=5  
  67.       begin  
  68.         select @r=left(@r,len(@r)-1)  
  69.         select @r=@r+'万'  
  70.       end  
  71.     end  
  72.         
  73.     select @a=@a+1,@b=@b-1  
  74.   end  
  75.      
  76.   if @xs='00'  
  77.   begin  
  78.      select @r=replace(@r+'元整','零元','元')  
  79.      return @r  
  80.   end  
  81.      
  82.   select @r=replace(@r+'元','零元','元')  
  83.      
  84.   if substring(@xs,1,1)<>'0'  
  85.   begin  
  86.      select @r=@r+isnull((select top 1 d from @gc where i=cast(substring(@xs,1,1) as tinyint)),'')+'角'  
  87.   end  
  88.      
  89.   if substring(@xs,2,1)<>'0'  
  90.   begin  
  91.      select @r=@r+isnull((select top 1 d from @gc where i=cast(substring(@xs,2,1) as tinyint)),'')+'分'  
  92.   end  
  93.   else  
  94.   begin  
  95.      select @r=@r+'整'  
  96.   end  
  97.      
  98.   return @r  
  99. end  
  100.    
  101.    
  102. -- 测试  
  103. select dbo.TCN('1335.32''x1',dbo.TCN('533100.3''x2'  
  104.    
  105. /*  
  106. x1                              x2  
  107. ------------------------------ ------------------------------  
  108. 壹仟叁佰叁拾伍元叁角贰分           伍拾叁万叁仟壹佰元叁角整  
  109.    
  110. (1 row(s) affected)  
  111. */  

猜你喜欢

转载自blog.csdn.net/hzp666/article/details/80268654
今日推荐