gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 20:54:34 24 4
gpt4 key购买 nike

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

old value =     500

new value = FIVE HUNDERED/=

最佳答案

使用力卢克;)
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

编辑 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?


根据您想要的身份位数,它可能是 RPADed与 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
输出:
+-------------+------------------------+
| OLD_VALUE | NEW_VALUE |
+-------------+------------------------+
| 10.03 | TEN POINT THREE |
| 10.3 | TEN POINT THIRTY |
| 10.33 | TEN POINT THIRTY-THREE |
+-------------+------------------------+

关于sql - 如何将数字转换为单词 - ORACLE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32962508/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com