将数值转换为单词时出错 - Oracle

时间:2016-07-15 06:17:39

标签: sql oracle

我试图将数值转换为单词。我成功完成了七个数值,如:1069000.使用以下sql:

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

但是当我再增加一个零时,如:10690000,它会给我以下错误:

日期格式图片在转换整个输入字符串之前结束

请任何人帮助将数值转换为超过八个字符的字词,例如 10690000

提前致谢

2 个答案:

答案 0 :(得分:0)

您的方法有一些限制:对于拼写数大于7的数字字符,您应该使用自己的函数。令人高兴的是,Tom Kyte已经写过了。

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

我在这里重复代码,因为网站不可用或链接会被删除:

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;
/

答案 1 :(得分: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