Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
684 views
in Technique[技术] by (71.8m points)

sql - How to convert number to words - ORACLE

I have written a very simple query which result a value 500, i need to convert this value like following:-

old value =     500

new value =  FIVE HUNDERED/=
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use the force Luke ;)

SqlFiddleDemo

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

The clue is Date in spelled 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

EDIT 3:

for 10.3 output is TEN POINT THREE but it should be TEN POINT THIRTY for 10.3 and TEN POINT THREE for 10.03. How could I achieve this?

Depending of how many digits you want for identity it could be RPADed with 0:

WITH cte AS
(
  SELECT 10.03 AS num FROM dual
  UNION ALL
  SELECT 10.30 FROM dual
  UNION ALL
  SELECT 10.33 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 (RPAD(SUBSTR(num, INSTR (num, '.') + 1)
                                                     ,2,'0')
                                                     ),'J'),'JSP')
     ELSE NULL
  END AS new_value
FROM cte;

db<>fiddle demo

Output:

+-------------+------------------------+
|  OLD_VALUE  |       NEW_VALUE        |
+-------------+------------------------+
|      10.03  | TEN POINT THREE        |
|       10.3  | TEN POINT THIRTY       |
|      10.33  | TEN POINT THIRTY-THREE |
+-------------+------------------------+

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...