将数字转换为单词 - 第一,第二,第三等

时间:2011-12-08 14:28:42

标签: sql sql-server string numbers

有没有人知道如何在SQL中取一个整数变量并将其转换为表格的等值数:First,Second,Third,Fourth等?

由于

6 个答案:

答案 0 :(得分:9)

这似乎是一种更简单的方法,一种很好的递归算法

CREATE FUNCTION fnIntegerToWords(@Number as BIGINT) 
    RETURNS VARCHAR(1024)
AS

BEGIN
      DECLARE @Below20 TABLE (ID int identity(0,1), Word varchar(32))
      DECLARE @Below100 TABLE (ID int identity(2,1), Word varchar(32))
      INSERT @Below20 (Word) VALUES 
                        ( 'Zero'), ('One'),( 'Two' ), ( 'Three'),
                        ( 'Four' ), ( 'Five' ), ( 'Six' ), ( 'Seven' ),
                        ( 'Eight'), ( 'Nine'), ( 'Ten'), ( 'Eleven' ),
                        ( 'Twelve' ), ( 'Thirteen' ), ( 'Fourteen'),
                        ( 'Fifteen' ), ('Sixteen' ), ( 'Seventeen'),
                        ('Eighteen' ), ( 'Nineteen' ) 

       INSERT @Below100 VALUES ('Twenty'), ('Thirty'),('Forty'), ('Fifty'),
                               ('Sixty'), ('Seventy'), ('Eighty'), ('Ninety')

    declare @belowHundred as varchar(126) 

    if @Number > 99 begin
        select @belowHundred = dbo.fnIntegerToWords( @Number % 100)
    end

    DECLARE @English varchar(1024) = 

    (

      SELECT Case 
        WHEN @Number = 0 THEN  ''

        WHEN @Number BETWEEN 1 AND 19 
          THEN (SELECT Word FROM @Below20 WHERE ID=@Number)

       WHEN @Number BETWEEN 20 AND 99   
         THEN  (SELECT Word FROM @Below100 WHERE ID=@Number/10)+ '-' +
               dbo.fnIntegerToWords( @Number % 10) 

       WHEN @Number BETWEEN 100 AND 999   
         THEN  (dbo.fnIntegerToWords( @Number / 100)) +' Hundred '+
             Case WHEN @belowHundred <> '' THEN 'and ' + @belowHundred else @belowHundred end 

       WHEN @Number BETWEEN 1000 AND 999999   
         THEN  (dbo.fnIntegerToWords( @Number / 1000))+' Thousand '+
             dbo.fnIntegerToWords( @Number % 1000)  

       WHEN @Number BETWEEN 1000000 AND 999999999   
         THEN  (dbo.fnIntegerToWords( @Number / 1000000))+' Million '+
             dbo.fnIntegerToWords( @Number % 1000000) 

       WHEN @Number BETWEEN 1000000000 AND 999999999999   
         THEN  (dbo.fnIntegerToWords( @Number / 1000000000))+' Billion '+
             dbo.fnIntegerToWords( @Number % 1000000000) 

            ELSE ' INVALID INPUT' END
    )

    SELECT @English = RTRIM(@English)

    SELECT @English = RTRIM(LEFT(@English,len(@English)-1))
        WHERE RIGHT(@English,1)='-'

    RETURN (@English)

END 

答案 1 :(得分:4)

您需要使用CASE声明:

SELECT intfield CASE WHEN 1 THEN 'First'
                     WHEN 2 THEN 'Second'
                     WHEN 3 THEN 'Third'
                     ...other conversions...
                     ELSE '' END
FROM Mytable

修改

下面3个字符数的工作代码。您可以在其下面添加所需的代码,用于2个和1个字符数字(基本上只需修改我写的内容并相应地更改SUBSTRING)。

基本上它将数字转换为字符串,检查有多少数字,然后相应地将其分解。

DECLARE @n int = 515

DECLARE @v varchar(100) = CAST(@N as varchar)

SELECT CASE WHEN LEN(@v) = 3 THEN 
    (CASE   WHEN LEFT(@v, 1) = 1 THEN 'One Hundred'
            WHEN LEFT(@v, 1) = 2 THEN 'Two Hundred'
            WHEN LEFT(@v, 1) = 3 THEN 'Three Hundred'
            WHEN LEFT(@v, 1) = 4 THEN 'Four Hundred'
            WHEN LEFT(@v, 1) = 5 THEN 'Five Hundred'
            WHEN LEFT(@v, 1) = 6 THEN 'Six Hundred'
            WHEN LEFT(@v, 1) = 7 THEN 'Seven Hundred'
            WHEN LEFT(@v, 1) = 8 THEN 'Eight Hundred'
            WHEN LEFT(@v, 1) = 9 THEN 'Nine Hundred'
            ELSE '' END)
    + ' ' +
    (CASE   WHEN SUBSTRING(@v, 2, 1) = 2 THEN 'Twenty'
            WHEN SUBSTRING(@v, 2, 1) = 3 THEN 'Thirty'
            WHEN SUBSTRING(@v, 2, 1) = 4 THEN 'Forty'
            WHEN SUBSTRING(@v, 2, 1) = 5 THEN 'Fifty'
            WHEN SUBSTRING(@v, 2, 1) = 6 THEN 'Sixty'
            WHEN SUBSTRING(@v, 2, 1) = 7 THEN 'Seventy'
            WHEN SUBSTRING(@v, 2, 1) = 8 THEN 'Eighty'
            WHEN SUBSTRING(@v, 2, 1) = 9 THEN 'Ninety'
            WHEN SUBSTRING(@v, 2, 1) = 1 THEN 
                CASE    WHEN SUBSTRING(@v, 2, 2) = 10 THEN 'Ten'
                        WHEN SUBSTRING(@v, 2, 2) = 11 THEN 'Eleven'
                        WHEN SUBSTRING(@v, 2, 2) = 12 THEN 'Twelve'
                        WHEN SUBSTRING(@v, 2, 2) = 13 THEN 'Thirteen'
                        WHEN SUBSTRING(@v, 2, 2) = 14 THEN 'Fourteen'
                        WHEN SUBSTRING(@v, 2, 2) = 15 THEN 'Fifteen'
                        WHEN SUBSTRING(@v, 2, 2) = 16 THEN 'Sixteen'
                        WHEN SUBSTRING(@v, 2, 2) = 17 THEN 'Seventeen'
                        WHEN SUBSTRING(@v, 2, 2) = 18 THEN 'Eighteen'
                        WHEN SUBSTRING(@v, 2, 2) = 19 THEN 'Nineteen'
                        ELSE '' END




            ELSE '' END)
    + ' ' +
    (CASE   WHEN SUBSTRING(@v, 2, 1) = 1 THEN ''
            WHEN SUBSTRING(@v, 3, 1) = 2 THEN 'Two'
            WHEN SUBSTRING(@v, 3, 1) = 3 THEN 'Three'
            WHEN SUBSTRING(@v, 3, 1) = 4 THEN 'Four'
            WHEN SUBSTRING(@v, 3, 1) = 5 THEN 'Five'
            WHEN SUBSTRING(@v, 3, 1) = 6 THEN 'Six'
            WHEN SUBSTRING(@v, 3, 1) = 7 THEN 'Seven'
            WHEN SUBSTRING(@v, 3, 1) = 8 THEN 'Eight'
            WHEN SUBSTRING(@v, 3, 1) = 9 THEN 'Nine'
            WHEN SUBSTRING(@v, 3, 1) = 1 THEN 'One'
            ELSE '' END)
    END

答案 2 :(得分:1)

此外,您可以CREATE TABLE使用数字及其名称。

CREATE TABLE tblNumbers (Nmb int, NmbWord varchar(100))
INSERT INTO tblNumbers 
VALUES (1,'first'), (2,'second'),(3,'third'), (4,'forth')

CREATE TABLE SomeTable (nmb int)

INSERT INTO SomeTable 
VALUES (1), (1),(2),(3)

SELECT  N.*
FROM tblNumbers N
     JOIN SomeTable ST ON ST.nmb=N.Nmb

DROP TABLE tblNumbers
DROP TABLE SomeTable

答案 3 :(得分:0)

没有固有的功能可识别数字并将其转换为单词。您需要根据业务规则构建一个case语句或存储过程/函数来为您完成。

http://www.novicksoftware.com/udfofweek/Vol2/T-SQL-UDF-Vol-2-Num-9-udf_Num_ToWords.htm

答案 4 :(得分:0)

这应该可以达到999 ......

IF OBJECT_ID('tempdb..#numbers_list') IS NOT NULL DROP TABLE #numbers_list
CREATE TABLE #numbers_list (int_value INT, text_value VARCHAR(100))
INSERT INTO #numbers_list
SELECT 1 as int_value, 'One' as text_value UNION ALL
SELECT 2 as int_value, 'Two' as text_value UNION ALL
SELECT 3 as int_value, 'Three' as text_value UNION ALL
SELECT 4 as int_value, 'Four' as text_value UNION ALL
SELECT 5 as int_value, 'Five' as text_value UNION ALL
SELECT 6 as int_value, 'Six' as text_value UNION ALL
SELECT 7 as int_value, 'Seven' as text_value UNION ALL
SELECT 8 as int_value, 'Eight' as text_value UNION ALL
SELECT 9 as int_value, 'Nine' as text_value UNION ALL
SELECT 10 as int_value, 'Ten' as text_value UNION ALL
SELECT 11 as int_value, 'Eleven' as text_value UNION ALL
SELECT 12 as int_value, 'Twelve' as text_value UNION ALL
SELECT 13 as int_value, 'Thirteen' as text_value UNION ALL
SELECT 14 as int_value, 'Fourteen' as text_value UNION ALL
SELECT 15 as int_value, 'Fifteen' as text_value UNION ALL
SELECT 16 as int_value, 'Sixteen' as text_value UNION ALL
SELECT 17 as int_value, 'Seventeen' as text_value UNION ALL
SELECT 18 as int_value, 'Eighteen' as text_value UNION ALL
SELECT 19 as int_value, 'Nineteen' as text_value UNION ALL
SELECT 20 as int_value, 'Twenty' as text_value UNION ALL
SELECT 30 as int_value, 'Thirty' as text_value UNION ALL
SELECT 40 as int_value, 'Fourty' as text_value UNION ALL
SELECT 50 as int_value, 'Fifty' as text_value UNION ALL
SELECT 60 as int_value, 'Sixty' as text_value UNION ALL
SELECT 70 as int_value, 'Seventy' as text_value UNION ALL
SELECT 80 as int_value, 'Eighty' as text_value UNION ALL
SELECT 90 as int_value, 'Ninety' as text_value UNION ALL
SELECT 100 AS int_value, 'One Hundred' AS text_value

SELECT 
tst2.number
,CASE WHEN exact.text_value IS NOT NULL THEN exact.text_value 
        WHEN tst2.number > 100 AND COALESCE(tens.text_value,ones.text_value) IS NULL THEN hunds.text_value + ' Hundred'
        WHEN tst2.number > 100 AND tst2.number_of_ones = 0 THEN hunds.text_value + ' Hundred And ' + tens.text_value
        WHEN tst2.number > 100 AND tst2.number_of_tens = 0 THEN hunds.text_value + ' Hundred And ' + ones.text_value
        WHEN tst2.number > 100 THEN hunds.text_value + ' Hundred And ' + tens.text_value + ' ' + ones.text_value
        WHEN tst2.number > 10 THEN tens.text_value + ' ' + ones.text_value
END AS string
FROM
(
SELECT tst.number
,tst.number % 10 as number_of_ones
,CASE WHEN tst.number < 101 THEN tst.number - (tst.number % 10) 
    WHEN tst.number > 100 THEN tst.number - 100 * (FLOOR(tst.number / 100)) - (tst.number % 10)
END as number_of_tens
,tst.number / 100 as number_of_hundreds
FROM 
(
SELECT 22 as number UNION ALL
SELECT 27 UNION ALL
SELECT 10 UNION ALL
SELECT 101 UNION ALL
SELECT 200 UNION ALL
SELECT 227
) tst
) tst2
LEFT OUTER JOIN #numbers_list exact on exact.int_value = tst2.number --if an exact match
LEFT OUTER JOIN #numbers_list ones on ones.int_value = tst2.number_of_ones 
LEFT OUTER JOIN #numbers_list tens on tens.int_value = tst2.number_of_tens
LEFT OUTER JOIN #numbers_list hunds on hunds.int_value = tst2.number_of_hundreds

答案 5 :(得分:0)

我刚刚在MySql中更新了一个从999到999999999的函数

这是原始链接的链接:Reference

更新后的版本如下:

CREATE FUNCTION `number_to_string`(n INT) RETURNS varchar(1000) CHARSET latin1 BEGIN
    declare ans varchar(1000);
    declare dig1, dig2, dig3,dig4,dig5,dig6,dig7,dig8,dig9 int;

    set ans = '';
    set dig9 = floor(n/100000000);
    set dig8 = floor(n/10000000) - dig9*10;
    set dig7 = floor(n/1000000) -(floor(n/10000000)*10);
    set dig6 = floor(n/100000) - (floor(n/1000000)*10);
    set dig5 = floor(n/10000) -  (floor(n/100000)*10);
    set dig4 = floor(n/1000) -   (floor(n/10000)*10);
    set dig3 = floor(n/100) -    (floor(n/1000)*10);
    set dig2 = floor(n/10) -     (floor(n/100)*10);
    set dig1 = n - (floor(n / 10)*10);

如果dig9&gt; 0然后

        case
            when dig9=1 then set ans=concat(ans, 'One Hundred');
            when dig9=2 then set ans=concat(ans, 'Two Hundred');
            when dig9=3 then set ans=concat(ans, 'Three Hundred');
            when dig9=4 then set ans=concat(ans, 'Four Hundred');
            when dig9=5 then set ans=concat(ans, 'Five Hundred');
            when dig9=6 then set ans=concat(ans, 'Six Hundred');
            when dig9=7 then set ans=concat(ans, 'Seven Hundred');
            when dig9=8 then set ans=concat(ans, 'Eight Hundred');
            when dig9=9 then set ans=concat(ans, 'Nine Hundred');
            else set ans = ans;
        end case;
         if ans <> '' and dig8 =0 and dig7 = 0 then
            set ans=concat(ans, ' Million');
        end if;
    end if;
    if ans <> '' and dig8 > 0 then
        set ans=concat(ans, ' And ');
    end if;
    if dig8 = 1 then
        case
            when (dig8*10 + dig7) = 10 then set ans=concat(ans,'Ten Million');
            when (dig8*10 + dig7) = 11 then set ans=concat(ans,'Eleven Million');
            when (dig8*10 + dig7) = 12 then set ans=concat(ans,'Twelve Million');
            when (dig8*10 + dig7) = 13 then set ans=concat(ans,'Thirteen Million');
            when (dig8*10 + dig7) = 14 then set ans=concat(ans,'Fourteen Million');
            when (dig8*10 + dig7) = 15 then set ans=concat(ans,'Fifteen Million');
            when (dig8*10 + dig7) = 16 then set ans=concat(ans,'Sixteen Million');
            when (dig8*10 + dig7) = 17 then set ans=concat(ans,'Seventeen Million');
            when (dig8*10 + dig7) = 18 then set ans=concat(ans,'Eighteen Million');
            when (dig8*10 + dig7) = 19 then set ans=concat(ans,'Nineteen Million');
            else set ans=ans;
        end case;
    else
        if dig8 > 0 then
            case
                when dig8=2 then set ans=concat(ans, ' Twenty');
                when dig8=3 then set ans=concat(ans, ' Thirty');
                when dig8=4 then set ans=concat(ans, ' Fourty');
                when dig8=5 then set ans=concat(ans, ' Fifty');
                when dig8=6 then set ans=concat(ans, ' Sixty');
                when dig8=7 then set ans=concat(ans, ' Seventy');
                when dig8=8 then set ans=concat(ans, ' Eighty');
                when dig8=9 then set ans=concat(ans, ' Ninety');
                else set ans=ans;
            end case;
            if ans <> '' and dig7 =0 then
            set ans=concat(ans, ' Million');
            end if;
        end if;
        if ans <> '' and dig7 > 0 and dig8 =0 then
        set ans=concat(ans, ' And ');
        end if;
        if dig7 > 0 then
        case
            when dig7=1 then set ans=concat(ans, ' One Million');
            when dig7=2 then set ans=concat(ans, ' Two Million');
            when dig7=3 then set ans=concat(ans, ' Three Million');
            when dig7=4 then set ans=concat(ans, ' Four Million');
            when dig7=5 then set ans=concat(ans, ' Five Million');
            when dig7=6 then set ans=concat(ans, ' Six Million');
            when dig7=7 then set ans=concat(ans, ' Seven Million');
            when dig7=8 then set ans=concat(ans, ' Eight Million');
            when dig7=9 then set ans=concat(ans, ' Nine Million');
            else set ans = ans;
        end case;
    end if;
end if;
    if ans <> '' and dig6 > 0 then
        set ans=concat(ans, ' And ');
    end if;

    if dig6 > 0 then
        case
            when dig6=1 then set ans=concat(ans, 'One Hundred');
            when dig6=2 then set ans=concat(ans, 'Two Hundred');
            when dig6=3 then set ans=concat(ans, 'Three Hundred');
            when dig6=4 then set ans=concat(ans, 'Four Hundred');
            when dig6=5 then set ans=concat(ans, 'Five Hundred');
            when dig6=6 then set ans=concat(ans, 'Six Hundred');
            when dig6=7 then set ans=concat(ans, 'Seven Hundred');
            when dig6=8 then set ans=concat(ans, 'Eight Hundred');
            when dig6=9 then set ans=concat(ans, 'Nine Hundred');
            else set ans = ans;
        end case;
         if ans <> '' and dig4 =0 and dig5 = 0 then
            set ans=concat(ans, ' Thousand');
        end if;
    end if;
    if ans <> '' and dig5 > 0 then
        set ans=concat(ans, ' And ');
    end if;
    if dig5 = 1 then
        case
            when (dig5*10 + dig4) = 10 then set ans=concat(ans,'Ten Thousand');
            when (dig5*10 + dig4) = 11 then set ans=concat(ans,'Eleven Thousand');
            when (dig5*10 + dig4) = 12 then set ans=concat(ans,'Twelve Thousand');
            when (dig5*10 + dig4) = 13 then set ans=concat(ans,'Thirteen Thousand');
            when (dig5*10 + dig4) = 14 then set ans=concat(ans,'Fourteen Thousand');
            when (dig5*10 + dig4) = 15 then set ans=concat(ans,'Fifteen Thousand');
            when (dig5*10 + dig4) = 16 then set ans=concat(ans,'Sixteen Thousand');
            when (dig5*10 + dig4) = 17 then set ans=concat(ans,'Seventeen Thousand');
            when (dig5*10 + dig4) = 18 then set ans=concat(ans,'Eighteen Thousand');
            when (dig5*10 + dig4) = 19 then set ans=concat(ans,'Nineteen Thousand');
            else set ans=ans;
        end case;
    else
        if dig5 > 0 then
            case
                when dig5=2 then set ans=concat(ans, ' Twenty');
                when dig5=3 then set ans=concat(ans, ' Thirty');
                when dig5=4 then set ans=concat(ans, ' Fourty');
                when dig5=5 then set ans=concat(ans, ' Fifty');
                when dig5=6 then set ans=concat(ans, ' Sixty');
                when dig5=7 then set ans=concat(ans, ' Seventy');
                when dig5=8 then set ans=concat(ans, ' Eighty');
                when dig5=9 then set ans=concat(ans, ' Ninety');
                else set ans=ans;
            end case;
            if ans <> '' and dig4 =0 then
            set ans=concat(ans, ' Thousand');
            end if;
        end if;
        if ans <> '' and dig4 > 0 and dig5 =0 then
        set ans=concat(ans, ' And ');
        end if;
        if dig4 > 0 then
        case
            when dig4=1 then set ans=concat(ans, ' One Thousand');
            when dig4=2 then set ans=concat(ans, ' Two Thousand');
            when dig4=3 then set ans=concat(ans, ' Three Thousand');
            when dig4=4 then set ans=concat(ans, ' Four Thousand');
            when dig4=5 then set ans=concat(ans, ' Five Thousand');
            when dig4=6 then set ans=concat(ans, ' Six Thousand');
            when dig4=7 then set ans=concat(ans, ' Seven Thousand');
            when dig4=8 then set ans=concat(ans, ' Eight Thousand');
            when dig4=9 then set ans=concat(ans, ' Nine Thousand');
            else set ans = ans;
        end case;
    end if;
end if;
    if ans <> '' and dig3 > 0 then
        set ans=concat(ans, ' And ');
    end if;
    if dig3 > 0 then
        case
            when dig3=1 then set ans=concat(ans, 'One Hundred');
            when dig3=2 then set ans=concat(ans, 'Two Hundred');
            when dig3=3 then set ans=concat(ans, 'Three Hundred');
            when dig3=4 then set ans=concat(ans, 'Four Hundred');
            when dig3=5 then set ans=concat(ans, 'Five Hundred');
            when dig3=6 then set ans=concat(ans, 'Six Hundred');
            when dig3=7 then set ans=concat(ans, 'Seven Hundred');
            when dig3=8 then set ans=concat(ans, 'Eight Hundred');
            when dig3=9 then set ans=concat(ans, 'Nine Hundred');
            else set ans = ans;
        end case;
    end if;
    if ans <> '' and dig2 > 0 then
        set ans=concat(ans, ' And ');
    end if;
    if dig2 = 1 then
        case
            when (dig2*10 + dig1) = 10 then set ans=concat(ans,'Ten');
            when (dig2*10 + dig1) = 11 then set ans=concat(ans,'Eleven');
            when (dig2*10 + dig1) = 12 then set ans=concat(ans,'Twelve');
            when (dig2*10 + dig1) = 13 then set ans=concat(ans,'Thirteen');
            when (dig2*10 + dig1) = 14 then set ans=concat(ans,'Fourteen');
            when (dig2*10 + dig1) = 15 then set ans=concat(ans,'Fifteen');
            when (dig2*10 + dig1) = 16 then set ans=concat(ans,'Sixteen');
            when (dig2*10 + dig1) = 17 then set ans=concat(ans,'Seventeen');
            when (dig2*10 + dig1) = 18 then set ans=concat(ans,'Eighteen');
            when (dig2*10 + dig1) = 19 then set ans=concat(ans,'Nineteen');
            else set ans=ans;
        end case;
    else
        if dig2 > 0 then
            case
                when dig2=2 then set ans=concat(ans, ' Twenty');
                when dig2=3 then set ans=concat(ans, ' Thirty');
                when dig2=4 then set ans=concat(ans, ' Fourty');
                when dig2=5 then set ans=concat(ans, ' Fifty');
                when dig2=6 then set ans=concat(ans, ' Sixty');
                when dig2=7 then set ans=concat(ans, ' Seventy');
                when dig2=8 then set ans=concat(ans, ' Eighty');
                when dig2=9 then set ans=concat(ans, ' Ninety');
                else set ans=ans;
            end case;
        end if;
        if ans <> '' and dig1 > 0 and dig2 =0 then
        set ans=concat(ans, ' And ');
        end if;
        if dig1 > 0 then
            case
                when dig1=1 then set ans=concat(ans, ' One');
                when dig1=2 then set ans=concat(ans, ' Two');
                when dig1=3 then set ans=concat(ans, ' Three');
                when dig1=4 then set ans=concat(ans, ' Four');
                when dig1=5 then set ans=concat(ans, ' Five');
                when dig1=6 then set ans=concat(ans, ' Six');
                when dig1=7 then set ans=concat(ans, ' Seven');
                when dig1=8 then set ans=concat(ans, ' Eight');
                when dig1=9 then set ans=concat(ans, ' Nine');
                else set ans=ans;
            end case;
        end if;
    end if;

    return trim(ans);
    END

我测试了它:选择number_to_string(999999999)

和结果:'九十九九十九百九十九九十九九九九'