--人家的/*--将数字转换为大写

将数字转换为大写或金额样式
最大支持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

解决方案 »

  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
      

  2.   

    谢谢各位!!!
    呵呵,我是急用,一下子写又感觉费事。
    所以就来问问了。
    谢谢victorycyz(中海)
    谢谢zjcxc(邹建) 
    谢谢8992026(8992026)