Oracle PL / SQL:不使用to_date将数字转换为单词

时间:2018-03-25 23:23:32

标签: sql oracle plsql to-date

是否有一种简单的替代方法可以使用to_date函数将数字转换为Oracle中的单词,使用PL / SQL,如0 - >零,1->一,2->两等

我研究了很多并找到了以下流行的声明,但我要求在不使用to_date()的情况下也这样做 例如。

SELECT TO_CHAR(TO_DATE(500,'J'),'Jsp')) FROM dual;

1 个答案:

答案 0 :(得分:0)

为什么要求不使用TO_DATE?这似乎是一个奇怪的要求。这就像说"我想查询数据但不使用SELECT"。

事实上,如果问题是您希望能够超过使用TO_DATE方法允许的值,则可以轻松扩展

SQL> create or replace
  2  function spell_number( p_number in number )
  3  return varchar2
  4  as
  5  type myArray is table of varchar2(255);
  6  l_str myArray := myArray( '',
  7  ' thousand ', ' million ',
  8  ' billion ', ' trillion ',
  9  ' quadrillion ', ' quintillion ',
 10   ' sextillion ', ' septillion ',
 11   ' octillion ', ' nonillion ',
 12   ' decillion ', ' undecillion ',
 13   ' duodecillion ' );
 14
 15   l_num varchar2(50) default trunc( p_number );
 16   l_return varchar2(4000);
 17   begin
 18   for i in 1 .. l_str.count
 19   loop
 20   exit when l_num is null;
 21
 22   if ( substr(l_num, length(l_num)-2, 3) <> 0 )
 23   then
 24   l_return := to_char(
 25   to_date(
 26   substr(l_num, length(l_num)-2, 3),
 27   'J' ),
 28   'Jsp' ) || l_str(i) || l_return;
 29   end if;
 30   l_num := substr( l_num, 1, length(l_num)-3 );
 31   end loop;
 32
 33   return l_return;
 34   end;
 35  /

Function created.

SQL>
SQL> select spell_number( 12345678901234567890123456789012345678 ) from dual;
Twelve undecillion Three Hundred Forty-Five decillion Six Hundred Seventy-Eight nonillion Nine Hundr
ed One octillion Two Hundred Thirty-Four septillion Five Hundred Sixty-Seven sextillion Eight Hundre
d Ninety quintillion One Hundred Twenty-Three quadrillion Four Hundred Fifty-Six trillion Seven Hund
red Eighty-Nine billion Twelve million Three Hundred Forty-Five thousand Six Hundred Seventy-Eight