如何使用db2 sql查询将数字转换为单词

时间:2018-08-14 08:09:46

标签: db2

有什么方法可以像Oracle to_date函数中那样将日期转换为儒略数字并拼写数字。请帮助如何在DB2中做到这一点 例如:-从DUAL中选择to_char(to_date(009341,'J'),'JSP')“ WORDS”;

1 个答案:

答案 0 :(得分:0)

不是我所知道的。您可能需要自己动手。例如。总数将达到100。我将其作为练习,让读者创建第二个UDF,然后该UDF可以达到数十亿。

CREATE OR REPLACE FUNCTION HUNDRED(i int)
RETURNS VARCHAR(40) RETURN
    CASE WHEN I BETWEEN 1 AND 19 THEN
        decode(i,0,'',1,'One',2,'Two',3,'Three',4,'Four',5,'Five',6,'Six',7,'Seven',8,'Eight',9,'Nine'
             , 10,'Ten',11,'Eleven',12,'Twelve',13,'Thirteen',14,'Fourteen',15,'Fifteen'
             , 16,'Sixteen',17,'Seventeen',18,'Eighteen',19,'Nineteen')
         WHEN MOD(i,10) = 0 THEN
         decode(i, 20,'Twenty',30,'Thirty',40,'Forty',50,'Fifty',60,'Sixty',70,'Seventy',80,'Eighty',90,'Ninety',100,'Hundred')
        ELSE 
               decode(int(i/10),2,'Twenty',3,'Thirty',4,'Forty',5,'Fifty',6,'Sixty',7,'Seventy',8,'Eighty',9,'Ninety')
              || '-' || decode(mod(i,10),0,'',1,'One',2,'Two',3,'Three',4,'Four',5,'Five',6,'Six',7,'Seven',8,'Eight',9,'Nine')
     END

例如第二个这样的UDF将使您达到Nine Hundred and Ninety-Nine ..只需扩展逻辑即可满足成千上万个等等

CREATE OR REPLACE FUNCTION INT_TO_WORDS(i int)
RETURNS VARCHAR(80)
RETURN
    case when i > 99 then HUNDRED(i/100) || ' Hundred ' || case when mod(i,100) > 0 then 'and ' else '' end else '' END
 || HUNDRED(mod(i,100))