2016-07-15 64 views
0

Iam嘗試將數字值轉換爲單詞。通過使用下面的SQL 1069000.:我成功地做到了多達七個數值喜歡將數字值轉換爲單詞時出錯 - Oracle

**UPPER(TO_CHAR (TO_DATE ((1069000), 'j'), 'jsp'))in_words** 

但是,當我像增加多一個零:10690000它給了我下面的錯誤:

日期格式將整個輸入字符串之前圖片結束

請人幫忙數值轉換成具有超過八個字符的話就像

在此先感謝

+0

例如,「百個千萬五千六百ONLY」這樣,謝謝。 –

回答

0
I have got the solution, Thanks to all. Please view the solution below. 



SELECT upper(case 
        when length(floor(instr_amount)) > 12 then 
        TO_CHAR(TO_DATE(floor(floor(instr_amount)/1000000000000), 'j'), 
          'jsp') || ' TRILLION ' || 
        DECODE(substr(floor(instr_amount), length(floor(instr_amount)) - 11, 3), 
          0, 
          '', 
          (TO_CHAR(TO_DATE(to_number(substr(floor(instr_amount), 
                  length(floor(instr_amount)) - 11, 
                  3)), 
              'J'), 
            'JSP')) || ' BILLION ') || 
        DECODE(substr(floor(instr_amount), length(floor(instr_amount)) - 8, 3), 
          0, 
          '', 
          (TO_CHAR(TO_DATE(to_number(substr(floor(instr_amount), 
                  length(floor(instr_amount)) - 8, 
                  3)), 
              'J'), 
            'JSP')) || ' MILLION ') || 
        DECODE(substr(floor(instr_amount), length(floor(instr_amount)) - 5), 
          0, 
          '', 
          (TO_CHAR(TO_DATE(substr(floor(instr_amount), 
                length(floor(instr_amount)) - 5), 
              'J'), 
            'JSP'))) || 
        decode((instr_amount - floor(instr_amount)) * 100, 
          0, 
          '', 
          ' AND ' || 
          TO_CHAR(TO_DATE((instr_amount - floor(instr_amount)) * 100, 'j'), 
            'jsp') || ' CENTS') 
        when length(floor(instr_amount)) > 9 then 
        TO_CHAR(TO_DATE(floor(floor(instr_amount)/1000000000), 'j'), 'jsp') || 
        ' BILLION ' || 
        DECODE(substr(floor(instr_amount), length(floor(instr_amount)) - 8, 3), 
          0, 
          '', 
          (TO_CHAR(TO_DATE(to_number(substr(floor(instr_amount), 
                  length(floor(instr_amount)) - 8, 
                  3)), 
              'J'), 
            'JSP')) || ' MILLION ') || 
        DECODE(substr(floor(instr_amount), length(floor(instr_amount)) - 5), 
          0, 
          '', 
          (TO_CHAR(TO_DATE(substr(floor(instr_amount), 
                length(floor(instr_amount)) - 5), 
              'J'), 
            'JSP'))) || 
        decode((instr_amount - floor(instr_amount)) * 100, 
          0, 
          '', 
          ' AND ' || 
          TO_CHAR(TO_DATE((instr_amount - floor(instr_amount)) * 100, 'j'), 
            'jsp') || ' CENTS') 
        when length(floor(instr_amount)) > 7 then 
        TO_CHAR(TO_DATE(floor(floor(instr_amount)/1000000), 'j'), 'jsp') || 
        ' MILLION ' || 
        DECODE(substr(floor(instr_amount), length(floor(instr_amount)) - 5), 
          0, 
          '', 
          (TO_CHAR(TO_DATE(substr(floor(instr_amount), 
                length(floor(instr_amount)) - 5), 
              'J'), 
            'JSP'))) || 
        decode((instr_amount - floor(instr_amount)) * 100, 
          0, 
          '', 
          ' AND ' || 
          TO_CHAR(TO_DATE((instr_amount - floor(instr_amount)) * 100, 'j'), 
            'jsp') || ' CENTS') 
        else 
        decode((floor(instr_amount)), 
          0, 
          '', 
          ((TO_CHAR(TO_DATE((floor(instr_amount)), 'J'), 'JSP')))) || 
        decode((instr_amount - floor(instr_amount)) * 100, 
          0, 
          '', 
          ' AND ' || 
          TO_CHAR(TO_DATE((instr_amount - floor(instr_amount)) * 100, 'j'), 
            'jsp') || ' CENTS') 
       end) in_words FROM mytable 
0

您的方法有一些限制:對於拼寫數字大於7的數字字符,您應該使用自己的函數。令人高興的是,Tom Kyte已經寫下了它。

https://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:1407603857650

我這裏重複的代碼由於網站將unavaliable或鏈接將被刪除:

create or replace 
function spell_number(p_number in number) 
return varchar2 
    as 
     type myArray is table of varchar2(255); 
     l_str myArray := myArray('', 
          ' thousand ', ' million ', 
          ' billion ', ' trillion ', 
          ' quadrillion ', ' quintillion ', 
          ' sextillion ', ' septillion ', 
          ' octillion ', ' nonillion ', 
          ' decillion ', ' undecillion ', 
          ' duodecillion '); 

     l_num varchar2(50) default trunc(p_number); 
     l_return varchar2(4000); 
    begin 
     for i in 1 .. l_str.count 
     loop 
      exit when l_num is null; 

      if (substr(l_num, length(l_num)-2, 3) <> 0) 
      then 
      l_return := to_char(
          to_date(
           substr(l_num, length(l_num)-2, 3), 
           'J'), 
         'Jsp') || l_str(i) || l_return; 
      end if; 
      l_num := substr(l_num, 1, length(l_num)-3); 
     end loop; 

     return l_return; 
end; 
/