/*--将数字转换为大写

将数字转换为大写或金额样式
最大支持8位小数,如果要支持更多的小数,需要更改@num的定义 原作者不详--整理 邹建 2003.09--*//*--调用示例 select dbo.f_ch2num(12.98,1),dbo.f_ch2num(103056204.4567,0),dbo.f_ch2num(103056204.4567,1)
--*/
Create proc f_Ch2Num(
@num decimal(38,8), --要转换的数字
@isMoney bit --是否转换为金额
)-- Returns Nvarchar(4000)
AS
BEGIN 
declare @re Nvarchar(4000)
declare @tmpstr Nvarchar(4000)
,@M Nvarchar(4000)
,@K Nvarchar(4000)
,@I numeric(38,8)
,@J int,@lastJ int
,@LastV Nvarchar(10)
,@LastF Nvarchar(10)
,@LastE Nvarchar(10)
,@LastVE Nvarchar(10)
select @I=@num
select @tmpstr=N'零壹贰叁肆伍陆柒捌玖分角元拾佰仟万拾佰仟亿拾佰仟'
,@K=N''
,@M=cast(cast(@I*100 as bigint) as varchar(800))
,@J=len(@M)
,@LastVE=N'' while @J>=1
begin
set @LastF=substring(@tmpstr, cast(substring(@m,len(@M)-@j+1,1) as bigint)+1,1)
set @LastE=substring(@tmpstr,10+@J,1)
if @LastF<>N'零'
begin
if @LastV=N'零'
if (@lastJ>=7 and @j<=7) or (@lastJ>=11 and @j<=11 ) or (@lastJ>=3 and @j<=2)
if @J<=2 and @lastJ<=3
set @K=@K+@LastVE+@LastF+@LastE
else
set @K=@K+@LastVE+@LastV+@LastF+@LastE
else
set @K=@K+@LastV+@LastF+@LastE
else
set @K=@K+@LastF+@LastE
select @lastJ=@j,@LastVE=N''
end
else
begin
if @LastVE=N'' and @lastJ>11 set @LastVE=N'亿'
if @LastVE=N'' and @lastJ>7 and @lastJ<10 set @LastVE=N'万'
if @LastVE=N'' and @lastJ>3 and @lastJ<6 set @LastVE=N'元'
if @LastV<>N'零' set @lastJ=@j
end
set @LastV=@LastF
set @J=@J-1
end if @lastJ>=3 set @K=@K+N'元'
if @lastJ>=2 set @K=@K+N'整' set @re=@K
return(@re)
END

解决方案 »

  1.   

    哈哈,什么叫整理?http://search.csdn.net/expert/topic/57/5701/2003/8/18/2158578.xml
      

  2.   

    --drop proc ConvertToUpperCase
    /*declare @a varchar(50) --@a为欲转换的大写金额
    exec ConvertToUpperCase 1000000,@a output --将123456789.56转换成大写金额
    select @a --在QA中查看效果
    */
    /*declare @a varchar(50) --@a为欲转换的大写金额
    exec ConvertToUpperCase 1056031010000,@a output 
    select @a */
    if exists (select * from sysobjects where objectproperty(object_id('ConvertToUpperCase'), 'IsProcedure') = 1)
    drop procedure ConvertToUpperCase
    GOcreate procedure ConvertToUpperCase
    (@mExpense Money,
    @sRetUpperMoney varchar(50) OutPut)
    as
    begin
    declare @sExpense varchar(20),@sLittle varchar(4),@sFixed varchar(15)
    declare @nDot Integer,@nLenAll Integer,@sUpperMoney varchar(30),@nStep Integer
    declare @cChar char(1),@sUpperNumbers varchar(30),@sNumber char(2),@sTemp varchar(50)
    declare @sZero char(2),@nIsZeroLast Integer,@sth varchar(10),@nFixed Integer
    declare @sMoney char(2)
    if @mExpense=0
    begin
    select @sRetUpperMoney='零元'
    return
    end
    if @mExpense<0
    begin
    select @sMoney='负'
    select @mExpense=abs(@mExpense)
    end
    else select @sMoney=''select @sUpperMoney = '佰拾万仟佰拾亿仟佰拾万仟佰拾元' --加大范围,这里设计len(@sUpperMoney)<=15,再大这种算法就不适合了。
    select @sUpperNumbers= '玖捌柒陆伍肆叁贰壹零'
    select @sZero = '零'
    select @sExpense = convert(varchar(20),@mExpense)select @nDot = CharIndex('.',@sExpense)
    select @nLenAll = Len(@sExpense)if @nDot > 0
    begin
    select @sFixed = substring(@sExpense,1,@nDot -1)
    select @sLittle = substring(@sExpense,@nDot + 1,@nLenAll - @nDot)
    end
    else
    select @sFixed = @sExpense select @nFixed = Len(@sFixed) 
    select @nStep = @nFixed select @sTemp = ''
    select @nIsZeroLast = 0if @sFixed <> '0'
    while @nStep > 0
    begin --while @nStep > 0select @cChar = substring(@sFixed,@nStep,1) 
    select @sNumber = substring(@sUpperNumbers,10 - convert(int,@cChar),1) 
    select @sth = substring(@sUpperMoney,len(@sUpperMoney) - (Len(@sFixed) - @nStep),1) if @cChar <> '0'
    begin --if @cChar <> '0'
    if @nIsZeroLast = 0
    select @sTemp = @sNumber + @sth + @sTemp 
    if @nIsZeroLast = 1
    begin
    select @sTemp = @sNumber + @sth + @sZero + @sTemp
    select @nIsZeroLast = 0
    end
    if @nIsZeroLast = 2
    begin
    select @sTemp = @sNumber + @sth + @sTemp
    select @nIsZeroLast = 0
    end
    end --if @cChar <> '0'else --@cChar = '0'begin
    if @nIsZeroLast <> 2
    begin --@nIsZeroLast <> 2
    if (@sth = '万') or (@sth = '亿') or (@sth = '元')
    begin 
    if @nIsZeroLast = 1
    begin
    select @sTemp = @sth + @sZero + @sTemp
    if substring(@sTemp,1,3)='亿万零' --修改
    select @sTemp=stuff(@sTemp,1,3,'亿零')
    if substring(@sTemp,1,2)='万零' --修改
    select @sTemp=stuff(@sTemp,1,2,'零')
    select @nIsZeroLast = 2
    end
    else
    begin
    select @sTemp = @sth + @sTemp
    if substring(@sTemp,1,2)='万元' and (len(@sFixed)-@nStep)>5--修改
    select @sTemp=stuff(@sTemp,1,2,'元')
    select @nIsZeroLast = 2
    end
    end elseselect @nIsZeroLast = 1
    end else --@nIsZeroLast = 2begin
    if (@sth = '仟') 
    select @nIsZeroLast = 0
    end
    end --@nIsZeroLast <> 2select @nStep = @nStep - 1 end --while @nStep > 0declare @sTempLittle varchar(10),@sFirst char(1),@sSecond char(1)
    select @sFirst = '',@sSecond = ''
    if Len(@sLittle) = 2
    begin
    select @sFirst = substring(@sLittle,1,1),@sSecond = substring(@sLittle,2,1)
    endif Len(@sLittle) = 1
    begin
    select @sFirst = substring(@sLittle,1,1)
    endselect @sTempLittle = ''
    if (@sFirst <> '') and (@sFirst <> '0')
    begin
    select @sNumber = substring(@sUpperNumbers,10 - convert(int,@sFirst),1)
    select @sTempLittle = @sTempLittle + @sNumber + '角'
    endif (@sSecond <> '') and (@sSecond <> '0')
    begin
    select @sNumber = substring(@sUpperNumbers,10 - convert(int,@sSecond),1)
    select @sTempLittle = @sTempLittle + @sNumber + '分'
    end
    select @sRetUpperMoney = @sMoney + @sTemp + @sTempLittle
    end
      

  3.   

    --我的函数显然不是那个帖子上来的,当然是看起来很乱,所以规范化了一下. --如果是谁的大作,请说一下.--上面有些问题,要改一下.
    /*--将数字转换为大写

    将数字转换为大写或金额样式
    最大支持8位小数,如果要支持更多的小数,需要更改@num的定义 原作者不详--整理 邹建 2003.09--*//*--调用示例 select dbo.f_ch2num(12.98,1),dbo.f_ch2num(103056204.4567,0),dbo.f_ch2num(103056204.4567,1)
    --*/
    Create function f_Ch2Num(
    @num decimal(38,8), --要转换的数字
    @isMoney bit --是否转换为金额
    ) Returns Nvarchar(4000)
    AS
    BEGIN 
    declare @re Nvarchar(4000)
    declare @tmpstr Nvarchar(4000)
    ,@M Nvarchar(4000)
    ,@K Nvarchar(4000)
    ,@I numeric(38,8)
    ,@J int,@lastJ int
    ,@LastV Nvarchar(10)
    ,@LastF Nvarchar(10)
    ,@LastE Nvarchar(10)
    ,@LastVE Nvarchar(10)
    select @I=@num
    select @tmpstr=N'零壹贰叁肆伍陆柒捌玖分角元拾佰仟万拾佰仟亿拾佰仟'
    ,@K=N''
    ,@M=cast(cast(@I*100 as bigint) as varchar(800))
    ,@J=len(@M)
    ,@LastVE=N'' while @J>=1
    begin
    set @LastF=substring(@tmpstr, cast(substring(@m,len(@M)-@j+1,1) as bigint)+1,1)
    set @LastE=substring(@tmpstr,10+@J,1)
    if @LastF<>N'零'
    begin
    if @LastV=N'零'
    if (@lastJ>=7 and @j<=7) or (@lastJ>=11 and @j<=11 ) or (@lastJ>=3 and @j<=2)
    if @J<=2 and @lastJ<=3
    set @K=@K+@LastVE+@LastF+@LastE
    else
    set @K=@K+@LastVE+@LastV+@LastF+@LastE
    else
    set @K=@K+@LastV+@LastF+@LastE
    else
    set @K=@K+@LastF+@LastE
    select @lastJ=@j,@LastVE=N''
    end
    else
    begin
    if @LastVE=N'' and @lastJ>11 set @LastVE=N'亿'
    if @LastVE=N'' and @lastJ>7 and @lastJ<10 set @LastVE=N'万'
    if @LastVE=N'' and @lastJ>3 and @lastJ<6 set @LastVE=N'元'
    if @LastV<>N'零' set @lastJ=@j
    end
    set @LastV=@LastF
    set @J=@J-1
    end if @lastJ>=3 set @K=@K+N'元'
    if @lastJ>=2 set @K=@K+N'整' set @re=@K
    return(@re)
    END
      

  4.   

    帖个最简洁的:
    CREATE FUNCTION [dbo].[f_num_chn] (@num numeric(14,2))
    RETURNS varchar(100) WITH ENCRYPTION
    AS
    BEGIN
      DECLARE @n_data VARCHAR(20),@c_data VARCHAR(100),@n_str VARCHAR(10),@i int
      SET @n_data=RIGHT(SPACE(14)+CAST(CAST(ABS(@num*100) AS bigint) AS varchar(20)),14)
      SET @c_data=''
      SET @i=1
      WHILE @i<=14
      BEGIN
        SET @n_str=SUBSTRING(@n_data,@i,1)
        IF @n_str<>' '
        BEGIN
          IF not ((SUBSTRING(@n_data,@i,2)='00') or
            ((@n_str='0') and ((@i=4) or (@i=8) or (@i=12) or (@i=14))))
            SET @c_data=@c_data+SUBSTRING('零壹贰叁肆伍陆柒捌玖',CAST(@n_str AS int)+1,1)
          IF not ((@n_str='0') and (@i<>4) and (@i<>8) and (@i<>12))
            SET @c_data=@c_data+SUBSTRING('仟佰拾亿仟佰拾万仟佰拾圆角分',@i,1)
          IF SUBSTRING(@c_data,LEN(@c_data)-1,2)='亿万'
            SET @c_data=SUBSTRING(@c_data,1,LEN(@c_data)-1)
        END
        SET @i=@i+1
      END
      IF @num<0
        SET @c_data='(负数)'+@c_data
      IF @num=0
        SET @c_data='零圆'
      IF @n_str='0'
        SET @c_data=@c_data+'整'
      RETURN(@c_data)
    END