如何将数字转换为单词 - ORACLE

时间:2015-10-06 05:43:15

标签: sql oracle

我写了一个非常简单的查询,结果值为500,我需要转换这个值如下: -

old value =     500

new value =  FIVE HUNDERED/=

2 个答案:

答案 0 :(得分:5)

使用力量卢克;)

<强> SqlFiddleDemo

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

线索是拼写格式的Date

修改

添加对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

编辑2:

为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

答案 1 :(得分:2)

您可以使用 J - &gt; JSP 技巧:

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

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

要了解其工作原理,请查看this explanation by Thomas Kyte