【问题标题】:Error converting numeric values into words - Oracle将数值转换为单词时出错 - Oracle
【发布时间】:2016-07-15 15:19:11
【问题描述】:

我正在尝试将数值转换为单词。我成功完成了最多七个数值,例如:1069000。使用以下 sql:

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

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

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

请任何人帮助将数值转换为具有八个以上字符的单词,例如 10690000

提前致谢

【问题讨论】:

  • 比如“一千万五千六百”这样的,谢谢。

标签: sql oracle


【解决方案1】:

您的方法有一些限制:对于大于 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;
/

【讨论】:

    【解决方案2】:
    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-05
      • 2012-07-15
      • 1970-01-01
      • 2013-01-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多