如何在sql server中写入数字到word函数

时间:2009-11-04 11:39:37

标签: sql-server tsql user-defined-functions

如何在SQL Server中编写一个函数来输出单词中的数字?

输入:1
输出:一个

输入:129
输出:一百二十九个

3 个答案:

答案 0 :(得分:1)

考虑使用辅助数字表。

注意:此MS SQL

创建一个序列表 - 这可能包括您需要的所有数字或至少高达999。 我限制它至少,但它增加了额外的逻辑。

CREATE TABLE [dbo].[Sequence]
    (
      seq INTEGER NOT NULL UNIQUE,
      word [varchar](25) NOT NULL
    )

INSERT INTO [Sequence] SELECT 0, ''
INSERT INTO [Sequence] SELECT 1, 'One'
INSERT INTO [Sequence] SELECT 2, 'Two'
INSERT INTO [Sequence] SELECT 3, 'Three'
INSERT INTO [Sequence] SELECT 4, 'Four'
INSERT INTO [Sequence] SELECT 5, 'Five'
INSERT INTO [Sequence] SELECT 6, 'Six'
INSERT INTO [Sequence] SELECT 7, 'Seven'
INSERT INTO [Sequence] SELECT 8, 'Eight'
INSERT INTO [Sequence] SELECT 9, 'Nine'

INSERT INTO [Sequence] SELECT 10, 'Ten'
INSERT INTO [Sequence] SELECT 11, 'Eleven'
INSERT INTO [Sequence] SELECT 12, 'Twelve'
INSERT INTO [Sequence] SELECT 13, 'Thirteen'
INSERT INTO [Sequence] SELECT 14, 'Fourteen'
INSERT INTO [Sequence] SELECT 15, 'Fifteen'
INSERT INTO [Sequence] SELECT 16, 'Sixteen'
INSERT INTO [Sequence] SELECT 17, 'Seventeen'
INSERT INTO [Sequence] SELECT 18, 'Eighteen'
INSERT INTO [Sequence] SELECT 19, 'Nineteen'

INSERT INTO [Sequence] SELECT 20, 'Twenty'
INSERT INTO [Sequence] SELECT 30, 'Thirty'
INSERT INTO [Sequence] SELECT 40, 'Forty'
INSERT INTO [Sequence] SELECT 50, 'Fifty'
INSERT INTO [Sequence] SELECT 60, 'Sixty'
INSERT INTO [Sequence] SELECT 70, 'Seventy'
INSERT INTO [Sequence] SELECT 80, 'Eighty'
INSERT INTO [Sequence] SELECT 90, 'Ninty'

然后创建用户定义的函数。

CREATE FUNCTION dbo.udf_NumToWords ( 
                @num AS INTEGER 
)       RETURNS VARCHAR(50)
AS
BEGIN

DECLARE @words AS VARCHAR(50)

IF      @num =     0 SELECT @words = 'Zero'
ELSE IF @num <    20 SELECT @words = word FROM sequence WHERE seq = @num
ELSE IF @num <   100 (SELECT @words = TTens.word + ' ' + TUnits.word 
                      FROM Sequence AS TUnits
                     CROSS JOIN Sequence AS TTens
                     WHERE TUnits.seq = (@num % 100) % 10
                       AND TTens.seq = (@num % 100) - (@num % 100) % 10 
                    )
ELSE IF @num =   100 (SELECT @words = THundreds.word + ' Hundred'
                      FROM Sequence AS THundreds
                     WHERE THundreds.seq = (@num / 100)
                    )
ELSE IF @num <  1000 (
                    SELECT @words = THundreds.word + ' Hundred and ' 
                                    + TTens.word + ' ' + TUnits.word 
                      FROM Sequence AS TUnits
                     CROSS JOIN Sequence AS TTens
                     CROSS JOIN Sequence AS THundreds
                     WHERE TUnits.seq = (@num % 100) % 10
                       AND TTens.seq = (@num % 100) - (@num % 100) % 10 
                       AND THundreds.seq = (@num / 100)
                    )
ELSE IF @num =  1000 (SELECT @words = TThousand.word + ' Thousand'
                      FROM Sequence AS TThousand
                     WHERE TThousand.seq = (@num / 1000)
                    )
ELSE IF @num < 10000 (
                    SELECT @words = TThousand.word + ' Thousand ' 
                                    + THundreds.word + ' Hundred and ' 
                                    + TTens.word + ' ' + TUnits.word 
                      FROM Sequence AS TUnits
                     CROSS JOIN Sequence AS TTens
                     CROSS JOIN Sequence AS THundreds
                     CROSS JOIN Sequence AS TThousand
                     WHERE TUnits.seq = (@num % 100) % 10
                       AND TTens.seq = (@num % 100) - (@num % 100) % 10 
                       AND THundreds.seq = (@num / 100) - (@num / 1000) * 10
                       AND TThousand.seq = (@num / 1000)
                    )
ELSE SELECT @words = STR(@num)

RETURN @words

END

现在测试功能:

SELECT NumberAsWords = dbo.udf_NumToWords(888);

答案 1 :(得分:1)

这是对Andrew的上述答案的略微修改(对安德鲁的所有信用)但这将在不使用任何函数的情况下返回sql查询中的结果

    WITH Sequence
AS (
    SELECT 0 seq
        ,'' word

    UNION ALL

    SELECT 1
        ,'One'

    UNION ALL

    SELECT 2
        ,'Two'

    UNION ALL

    SELECT 3
        ,'Three'

    UNION ALL

    SELECT 4
        ,'Four'

    UNION ALL

    SELECT 5
        ,'Five'

    UNION ALL

    SELECT 6
        ,'Six'

    UNION ALL

    SELECT 7
        ,'Seven'

    UNION ALL

    SELECT 8
        ,'Eight'

    UNION ALL

    SELECT 9
        ,'Nine'

    UNION ALL

    SELECT 10
        ,'Ten'

    UNION ALL

    SELECT 11
        ,'Eleven'

    UNION ALL

    SELECT 12
        ,'Twelve'

    UNION ALL

    SELECT 13
        ,'Thirteen'

    UNION ALL

    SELECT 14
        ,'Fourteen'

    UNION ALL

    SELECT 15
        ,'Fifteen'

    UNION ALL

    SELECT 16
        ,'Sixteen'

    UNION ALL

    SELECT 17
        ,'Seventeen'

    UNION ALL

    SELECT 18
        ,'Eighteen'

    UNION ALL

    SELECT 19
        ,'Nineteen'

    UNION ALL

    SELECT 20
        ,'Twenty'

    UNION ALL

    SELECT 30
        ,'Thirty'

    UNION ALL

    SELECT 40
        ,'Forty'

    UNION ALL

    SELECT 50
        ,'Fifty'

    UNION ALL

    SELECT 60
        ,'Sixty'

    UNION ALL

    SELECT 70
        ,'Seventy'

    UNION ALL

    SELECT 80
        ,'Eighty'

    UNION ALL

    SELECT 90
        ,'Ninty'
    )
    ,CTENumbers
AS (
    SELECT 0 AS num  --changezero to your starting number

    UNION ALL

    SELECT num + 1  
    FROM CTENumbers
    WHERE num + 1 <= 255 --change 255 to upper limit
    )
SELECT *
    ,CASE 
        WHEN num = 0
            THEN 'Zero'
        WHEN num < 20
            THEN (
                    SELECT word
                    FROM sequence
                    WHERE seq = num
                    )
        WHEN num < 100
            THEN (
                    SELECT TTens.word + ' ' + TUnits.word
                    FROM Sequence AS TUnits
                    CROSS JOIN Sequence AS TTens
                    WHERE TUnits.seq = (num % 100) % 10
                        AND TTens.seq = (num % 100) - (num % 100) % 10
                    )
        WHEN num < 1000
            THEN (
                    SELECT THundreds.word + ' Hundred and ' + TTens.word + ' ' + TUnits.word
                    FROM Sequence AS TUnits
                    CROSS JOIN Sequence AS TTens
                    CROSS JOIN Sequence AS THundreds
                    WHERE TUnits.seq = (num % 100) % 10
                        AND TTens.seq = (num % 100) - (num % 100) % 10
                        AND THundreds.seq = (num / 100)
                    )
        WHEN num = 1000
            THEN (
                    SELECT TThousand.word + ' Thousand'
                    FROM Sequence AS TThousand
                    WHERE TThousand.seq = (num / 1000)
                    )
        WHEN num < 10000
            THEN (
                    SELECT TThousand.word + ' Thousand ' + THundreds.word + ' Hundred and ' + TTens.word + ' ' + TUnits.word
                    FROM Sequence AS TUnits
                    CROSS JOIN Sequence AS TTens
                    CROSS JOIN Sequence AS THundreds
                    CROSS JOIN Sequence AS TThousand
                    WHERE TUnits.seq = (num % 100) % 10
                        AND TTens.seq = (num % 100) - (num % 100) % 10
                        AND THundreds.seq = (num / 100) - (num / 1000) * 10
                        AND TThousand.seq = (num / 1000)
                    )
        ELSE STR(num)
        END Number
FROM CTENumbers
OPTION (MAXRECURSION 10000)

答案 2 :(得分:0)

如果它可以帮助任何人,你可以创建一个数字表并使用像c#这样的服务器端语言来填充它。您还可以使用Humanizer库将数字转换为单词;它还支持本地化。

SQL TABLE

CREATE TABLE [Numbers](
[n] [bigint] NOT NULL,
[InWords] [nvarchar](50) NULL,
)

C#代码

需要Humanizer

var db = new MyDbContext();
for (int i = 1; i < 1000; i++)
{
db.Database.ExecuteSqlCommand($"INSERT INTO Numbers (n, InWords) VALUES({i}, '{i.ToWords()}')");
}