求用sql存储过程写将 小数(金额)转换成中文大写,急!

解决方案 »

  1.   

    -- --------调用示例
    -- declare @tt varchar(100)
    -- set @tt = dbo.F_Change(34546233.45)
    -- print @tt
    -- 
    -- ---create function F_Change(@Cost decimal(28,2))
    returns varchar(100)
    as
    begin
    declare @l_cost varchar(16)
    declare @lstr varchar(13)
    declare @rstr1 char(1)
    declare @rstr2 char(1)
    declare @lenth int
    declare @return varchar(100)
    set @return = ''
    set @l_cost = convert(varchar(16),round(@Cost,2))
    set @lstr = left(@l_cost,len(@l_cost) - 3)
    ----角
    set @rstr1 = left(right(@l_cost,2),1)
    ----分
    set @rstr2 = right(@l_cost,1)
    set @lenth = len(@lstr)
    declare @tb table(lenth int identity(1,1), num char(1))
    while @lenth > 0
    begin
    insert into @tb
    values(right(@lstr,1))
    set @lstr = left(@lstr,len(@lstr) - 1)
    set @lenth = @lenth - 1
    enddeclare @tb1 table(minnum char(1),maxnum char(2))
    insert into @tb1
    select '0','零'
    union select '1','壹'
    union   select '2','贰'
    union   select '3','叁'
    union   select '4','肆'
    union   select '5','伍'
    unionselect '6','陆'
    unionselect '7','柒'
    unionselect '8','捌'
    unionselect '9','玖'declare @tb2 table(lenth int,unit char(2))
    insert into @tb2
     select 1,'元'
    union   select 2,'拾'
    union   select 3,'佰'
    union   select 4,'仟'
    union   select 5,'万'
    unionselect 6,'拾'
    unionselect 7,'佰'
    unionselect 8,'仟'
    unionselect 9,'亿'
    unionselect 10,'拾'
    unionselect 11,'佰'
    unionselect 12,'仟'
    unionselect 13,'万'select @return = @return 
     + (select tb1.maxnum from @tb1 tb1 where tb.num = tb1.minnum)
     + (select tb2.unit from @tb2 tb2 where tb.lenth = tb2.lenth)
    from   @tb tb
    order by tb.lenth descset @return = @return + (select maxnum from @tb1 where minnum = @rstr1 ) + '角'
       + (select maxnum from @tb1 where minnum = @rstr2) + '分' + '整'
    return @return end