Oracle
 sql >> Base de données >  >> RDS >> Oracle

Comment convertir un nombre en mots - ORACLE

Utilise la force Luke;)

SqlFiddleDemo

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

L'indice est Date au format orthographié.

MODIF :

Ajout de la prise en charge des 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

MODIFICATION 2 :##

Ajout d'un support limité pour 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

MODIFICATION 3 :

pour 10.3, la sortie est DIX POINT TROIS, mais elle devrait être DIX POINT TRENTE pour 10.3 et DIX POINT TROIS pour 10.03. Comment pourrais-je y parvenir ?

Selon le nombre de chiffres que vous voulez pour l'identité, il pourrait être RPADed avec 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<>démo de violon

Sortie :

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