Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
![]() |
![]() |
Home -> Community -> Usenet -> c.d.o.tools -> Re: Change number to word
,In comp.databases.oracle.misc Theresa Wong <kywong_at_chinahkphoto.com.hk> wrote:
: Hi,
: Do anyone have soultion about changing number to word (123,456,789 --> One
: hundred million ......) in oracle SQL or PL/SQL.
: Regards,
: Theresa
Here's a solution once posted by Thomas Kyte to spell large numbers:
Sure, with a little effort -- you can use the J -> Jsp trick and do it yourself.
ps$tkyte_at_DEV816> 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 l_return := to_char( 23 to_date( 24 substr(l_num, length(l_num)-2, 3), 25 'J' ), 26 'Jsp' ) || l_str(i) || l_return; 27 28 l_num := substr( l_num, 1, length(l_num)-3 ); 29 end loop; 30 31 return l_return;
Function created.
ops$tkyte_at_DEV816>
ops$tkyte_at_DEV816> select
2 spell_number( 12345678901234567890123456789012345678 )
3 from dual;
SPELL_NUMBER(1234567890123456789012345678901234567
ops$tkyte_at_DEV816>
ops$tkyte_at_DEV816> declare
2 l_num number; 3 l_str varchar2(255); 4 l_spelled varchar2(4000); 5 begin 6 for i in 1 .. 10 7 loop 8 l_num := random.rand() || 9 random.rand() || 10 random.rand(); 11 l_str := 12 to_char( l_num, '999,999,999,999,999,999' ) ; 13 l_spelled := spell_number( l_num ); 14 15 dbms_output.put_line 16 ( l_str || ' -- ' || l_spelled ); 17 end loop;
PL/SQL procedure successfully completed.
ops$tkyte_at_DEV816>
Helen Received on Tue Jul 31 2001 - 03:10:01 CDT
![]() |
![]() |