0

我不知道我是否要求太多,但是。有没有办法将数字转换为英文的序号?例如:

5 --> 'five'
251 --> 'two hundred fifty one'
2549 --> 'two thousand five hundred forty nine'
4

1 回答 1

1

试试这个——

CREATE FUNCTION dbo.udf_getString(@num INT)
RETURNS VARCHAR(250)
AS BEGIN

    DECLARE @result VARCHAR(250)

    SELECT @result = REPLACE(t2.txt, '  ', ' ')
    FROM (
        SELECT txt =
            CASE t100000
                WHEN 1 THEN 'one'
                WHEN 2 THEN 'two'
                WHEN 3 THEN 'three'
                WHEN 4 THEN 'four'
                WHEN 5 THEN 'five'
                WHEN 6 THEN 'six'
                WHEN 7 THEN 'seven'
                WHEN 8 THEN 'eight'
                WHEN 9 THEN 'nine'
                ELSE ''
            END +
            CASE WHEN t.t100000 > 0 
                THEN ' hundred' 
                ELSE '' 
            END +
            CASE WHEN t1000 > 0 AND (@num / 1000) > 100 
                THEN ' and '
                ELSE '' 
            END +
            CASE t1000
                WHEN 10 THEN 'ten'
                WHEN 11 THEN 'eleven'
                WHEN 12 THEN 'twelve'
                WHEN 13 THEN 'thirteen'
                WHEN 14 THEN 'fourteen'
                WHEN 15 THEN 'fifteen'
                WHEN 16 THEN 'sixteen'
                WHEN 17 THEN 'seventeen'
                WHEN 18 THEN 'eighteen'
                WHEN 19 THEN 'nineteen'
                ELSE 
                    CASE t10000
                        WHEN 2 THEN 'twen'
                        WHEN 3 THEN 'thir'
                        WHEN 4 THEN 'for'
                        WHEN 5 THEN 'fif'
                        WHEN 6 THEN 'six'
                        WHEN 7 THEN 'seven'
                        WHEN 8 THEN 'eight'
                        WHEN 9 THEN 'nine'
                        ELSE ''
                    END +
                    CASE WHEN t10000 > 0 
                        THEN 'ty' 
                        ELSE '' 
                    END +
                    CASE WHEN t1000 > 0 AND t10000 > 0 
                        THEN ' ' 
                        ELSE '' 
                    END +
                    CASE t1000
                        WHEN 1 THEN 'one'
                        WHEN 2 THEN 'two'
                        WHEN 3 THEN 'three'
                        WHEN 4 THEN 'four'
                        WHEN 5 THEN 'five'
                        WHEN 6 THEN 'six'
                        WHEN 7 THEN 'seven'
                        WHEN 8 THEN 'eight'
                        WHEN 9 THEN 'nine'
                        ELSE ''
                    END
                END +
                CASE WHEN (@num / 1000) % 1000 > 0 
                    THEN ' thousand' 
                    ELSE '' 
                END +
                CASE WHEN t100 > 0 
                    THEN ' ' 
                    ELSE '' 
                END +
                CASE t100
                    WHEN 1 THEN 'one'
                    WHEN 2 THEN 'two'
                    WHEN 3 THEN 'three'
                    WHEN 4 THEN 'four'
                    WHEN 5 THEN 'five'
                    WHEN 6 THEN 'six'
                    WHEN 7 THEN 'seven'
                    WHEN 8 THEN 'eight'
                    WHEN 9 THEN 'nine'
                    ELSE ''
                END +
                CASE WHEN t100 > 0 
                    THEN ' hundred' 
                    ELSE '' 
                END +
                CASE WHEN @num % 100 > 0 AND @num > 100 
                    THEN ' and ' 
                    ELSE '' 
                END +
                CASE @num % 100
                    WHEN 10 THEN 'ten'
                    WHEN 11 THEN 'eleven'
                    WHEN 12 THEN 'twelve'
                    WHEN 13 THEN 'thirteen'
                    WHEN 14 THEN 'fourteen'
                    WHEN 15 THEN 'fifteen'
                    WHEN 16 THEN 'sixteen'
                    WHEN 17 THEN 'seventeen'
                    WHEN 18 THEN 'eighteen'
                    WHEN 19 THEN 'nineteen'
                    ELSE 
                        CASE t10
                            WHEN 2 THEN 'twen'
                            WHEN 3 THEN 'thir'
                            WHEN 4 THEN 'for'
                            WHEN 5 THEN 'fif'
                            WHEN 6 THEN 'six'
                            WHEN 7 THEN 'seven'
                            WHEN 8 THEN 'eight'
                            WHEN 9 THEN 'nine'
                            ELSE ''
                        END +
                        CASE WHEN t10 > 0 
                            THEN 'ty' 
                            ELSE '' 
                        END +
                        CASE WHEN @num % 10 > 0 AND t10 > 0 
                            THEN ' ' 
                            ELSE '' 
                        END +
                        CASE @num % 10
                            WHEN 1 THEN 'one'
                            WHEN 2 THEN 'two'
                            WHEN 3 THEN 'three'
                            WHEN 4 THEN 'four'
                            WHEN 5 THEN 'five'
                            WHEN 6 THEN 'six'
                            WHEN 7 THEN 'seven'
                            WHEN 8 THEN 'eight'
                            WHEN 9 THEN 'nine'
                            ELSE ''
                          END
                        END
        FROM (
            SELECT 
                  t100000 = (@num / 100000) % 10
                , t10000 = (@num / 10000) % 10
                , t1000 = (@num / 1000) % 100
                , t100 = (@num / 100) % 100
                , t10 = (@num / 10) % 10
        ) t
    ) t2

    RETURN @result

END
于 2013-04-27T08:44:58.640 回答