将数字转换为阿拉伯语中的单词

时间:2012-12-26 14:15:29

标签: sql sql-server

  

可能重复:
  recursive function.. won’t work again SQL

我创建了一个包含0到99之间数字的表格; 对于这两个数字之间的数字,查询正在工作, 它也适用于这些数字: 100,200,1000,2000,1100,2100,1000000。一般来说,它适用于数字> 100,并且不包含我已经创建的表中的数字。 而对于所有其他数字,我得到'null'。 我认为问题出现在变量@Units中,但我不能再关注它了。

CREATE FUNCTION dbo.fnIntegerToWords(@Nb as BIGINT) 
RETURNS NVARCHAR(1024)
AS
BEGIN
DECLARE @Millions as int
DECLARE @Remainder as int
DECLARE @Thousands as bigint
DECLARE @Hundreds as int
DECLARE @Units as int
DECLARE @word as nvarchar (1024)
DECLARE @result as nvarchar(1024)

set @Millions = @Nb/1000000
set @Units = @Nb %1000000
IF @Millions > 0
BEGIN
    if @Millions = 1
        set @result = N' مليون'

    if @Units > 0 
    begin
        set @Thousands = @Units / 1000
        set @Units = @Units % 1000

        if @Thousands > 0
        begin
            if @Thousands = 1
                set @result = @result + N' و' + N'ألف'
            else
            if @Thousands = 2
                set @result = @result + N' و' + N'ألفان'
            else
            if @Thousands between 100 and 199
            BEGIN
                set @Hundreds = @Thousands / 100
                set @Remainder = @Thousands % 100
                if @Remainder = 0
                begin
            set @result = @result + N' مائة ألف'
                    set @Thousands = 0
                end
                else
                if @Remainder > 0
                begin
                    set @result = @result + N' و' +  N'مائة'
                    if @Remainder > 0 
                        set @result = @result + N' و' + dbo.fnIntegerToWords(@Remainder) + N' ألف'
                end
            END
            else
            if @Thousands between 200 and 299
            BEGIN
                set @Hundreds = @Thousands / 100
                set @Remainder = @Thousands % 100
                if @Remainder = 0
                begin
                    set @result = @result + N' و' + N' مئتان ألف'
                    set @Thousands = 0
                end
                else
                if @Remainder > 0
                begin
                    set @result = @result + N' و' + N' مئتان'
                    if @Remainder > 0 
                        set @result = @result + N' و' + dbo.fnIntegerToWords(@Remainder) + N' ألف'
                end
            END
            else
            if @Thousands > 299
            BEGIN
                set @Hundreds = @Thousands / 100
                set @Remainder = @Thousands % 100
                if @Remainder = 0
                begin
                    set @result = @result + N' و' + dbo.fnIntegerToWords(@Hundreds) + N' مائة ألف'

                end
                else
                begin
                    set @result = @result + N' و' + dbo.fnIntegerToWords(@Hundreds) +  N' مائة' 

                    if @Remainder > 0
                        set @result = @result + N' و' + dbo.fnIntegerToWords(@Remainder)+ N' ألف'

                end     
            END 
            else
            if @Thousands between 3 and 99
                set @result = @result + N' و' + dbo.fnIntegerToWords(@Thousands) + N' ألف'

        END 
        if @Units > 0
        Begin
            set @Hundreds = @Units / 100
            set @Units = @Units % 100
            if @Hundreds > 0 
            BEGIN
                if @Hundreds = 1
                    set @result = @result + N' و' + N' مائة'
                else
                if @Hundreds = 2
                    set @result = @result + N' و' + N' مئتان'
                else
                    set @result = @result + N' و' + dbo.fnIntegerToWords(@Hundreds) + N' مائة'
            END
            if @Units > 0 
                set @result = @result + N' و' + dbo.fnIntegerToWords(@Units)                            
        End

    end
END
ELSE
IF @Millions = 0 
BEGIN
    set @Thousands = @Units /1000
    set @Units = @Units %1000

    IF @Thousands > 0
    BEGIN
        if @Thousands = 1
            set @result = N' ألف'
        else
        if @Thousands = 2
            set @result = N' ألفان'
        else
        if @Thousands between 100 and 199
        BEGIN
            set @Hundreds = @Thousands / 100
            set @Remainder = @Thousands % 100
            if @Remainder = 0
                set @result = N' مائة ألف'
            else
            if @Remainder > 0
            begin
                set @result = N' مائة'
                if @Remainder > 0 
                    set @result = @result + N' و' + dbo.fnIntegerToWords(@Remainder) + N' ألف'
            end
        END
        else
        if @Thousands between 200 and 299
        BEGIN
            set @Hundreds = @Thousands / 100
            set @Remainder = @Thousands % 100
            if @Remainder = 0
            begin
                set @result = N' مئتان ألف'
            end
            else
            if @Remainder > 0
            begin
                set @result = N' مئتان'
                if @Remainder > 0 
                    set @result = @result + N' و' + dbo.fnIntegerToWords(@Remainder) + N' ألف'
            end
        END
        else

        if @Thousands > 299
            BEGIN
                set @Hundreds = @Thousands / 100
                set @Remainder = @Thousands % 100
                if @Remainder = 0
                begin
                    set @result = dbo.fnIntegerToWords(@Hundreds) + N' مائة ألف'
                end
                else
                begin
                    set @result = dbo.fnIntegerToWords(@Hundreds) + N'         مائة'         
                    if @Remainder > 0
                        set @result = @result + N' و' + dbo.fnIntegerToWords(@Remainder) + N' ألف'

                end
            END 
        else
            set @result = dbo.fnIntegerToWords(@Thousands) + N' ألف' 
        if @Units <> 0 
        BEGIN
            set @Hundreds = @Units / 100

            set @Units = @Units % 100
            if @Hundreds > 0
            BEGIN
                if @Hundreds = 1
                    set @result = @result + N' و' + N' مائة'
                else
                if @Hundreds = 2
                    set @result = @result + N' و' + N' مئتان'
                else
                    set @result = @result + N' و' +  dbo.fnIntegerToWords(@Hundreds) + N' مائة'
            END
            if @Units > 0 
                set @result = @result + N' و' +  dbo.fnIntegerToWords(@Units)
        END
    END
END

ELSE

IF @Thousands = 0
BEGIN
    set @Hundreds = @Units / 100
    set @Units = @Units % 100
    if @Hundreds > 0 
    BEGIN
        if @Hundreds = 1
            set @result = N' مائة'
        else
        if @Hundreds = 2
            set @result = N' مئتان'
        else
            set @result = dbo.fnIntegerToWords(@Hundreds) + N' مائة'

        if @Units > 0 
            set @result = @result + N' و' + dbo.fnIntegerToWords(@Units)
    END
else
    set @result = (select COALESCE(word,'') from number where @Units=number)
END

RETURN @result
END

1 个答案:

答案 0 :(得分:0)

问题可能是您要附加到result,而不是初始化它。 NULL字符串不是空字符串(在大多数数据库中)。

尝试将声明更改为:

DECLARE @result as nvarchar(1024) = ''

或者,如果您使用的是旧版本的SQL Server:

DECLARE @result as nvarchar(1024);
SET @result = = ''