How to convert a number to words - ORACLE

I wrote a very simple query that returns a value of 500, I need to convert this value as shown below: -

old value =     500

new value =  FIVE HUNDERED/=
+4
source share
2 answers

Use the power of Luke;)

SqlFiddleDemo

SELECT UPPER(TO_CHAR(TO_DATE(500,'J'),'Jsp')) || '/=' AS new_value
FROM dual;  

Hint Datein recorded format.

EDIT:

Adding support for negative numbers:

SqlFiddleDemo

WITH cte AS
(
  SELECT 10 AS num      FROM dual
  UNION ALL SELECT -500 FROM dual
  UNION ALL SELECT 0    FROM dual
)
SELECT num AS old_value,
       decode( sign( num ), -1, 'NEGATIVE ', 0, 'ZERO', NULL ) ||
       decode( sign( abs(num) ), +1, to_char( to_date( abs(num),'J'),'JSP') ) || '/=' AS new_value
FROM cte

EDIT 2:

Adding limited support for float:

SqlFiddleDemo3

WITH cte AS
(
  SELECT 10 AS num       FROM dual
  UNION ALL SELECT -500  FROM dual
  UNION ALL SELECT 0     FROM dual
  UNION ALL SELECT 10.3  FROM dual
  UNION ALL SELECT -10.7 FROM dual
)
SELECT 
  num AS old_value,
  decode( sign( num ), -1, 'NEGATIVE ', 0, 'ZERO', NULL )
  || decode( sign( abs(num) ), +1, to_char( to_date( abs(TRUNC(num)),'J'),'JSP') )
  ||
  CASE
     WHEN INSTR (num, '.') > 0
     THEN  ' POINT ' || TO_CHAR (TO_DATE (TO_NUMBER (SUBSTR(num, INSTR (num, '.') + 1)),'J'),'JSP')
     ELSE NULL
  END AS new_value
FROM cte
+4
source

You can use the trick J β†’ JSP :

SQL> SELECT TO_CHAR(TO_DATE(500,'J'),'JSP')||'/=' num_2_words FROM dual;

NUM_2_WORDS
--------------
FIVE HUNDRED/=

To understand how this works, check out this Thomas Kyte explanation .

+1
source

Source: https://habr.com/ru/post/1610358/


All Articles