将数字转换为单词时ORA-01830

时间:2018-04-14 14:29:45

标签: sql oracle

高值是十进制格式,例如.- 100.10,我想把它转换为单词,所以我写下脚本但没有得到执行...

SELECT SYMBOL, HIGH, UPPER(TO_CHAR(TO_DATE(HIGH,'J'),'JSP'))
AMT_IN_WORDS FROM BHAV; 

错误

  

ORA-01830

请在错误的地方纠正这个....

提前谢谢你......

2 个答案:

答案 0 :(得分:1)

您可以创建一个功能。

CREATE OR REPLACE FUNCTION big_amt_in_words (p_input VARCHAR2) RETURN VARCHAR2
IS
   v_running_input NUMBER;
   v_num NUMBER;
   v_amt_in_words VARCHAR2(2000);
BEGIN
   v_running_input := P_input;
   FOR i IN (
             SELECT RPAD(1, (rownum*3)+1, 0) num_value,
                    CASE LENGTH(RPAD(1, (rownum*3)+1, 0))
                        WHEN 4 THEN 'THOUSAND'
                        WHEN 7 THEN 'MILLION'
                        WHEN 10 THEN 'BILLION'
                        WHEN 13 THEN 'TRILLION'
                        WHEN 16 THEN 'QUADRILLION'
                        WHEN 19 THEN 'QUINTILLION'
                        WHEN 22 THEN 'SEXTILLION'
                        WHEN 25 THEN 'SEPTILLION'
                        WHEN 28 THEN 'OCTILLION'
                    END place_value
              FROM DUAL
           CONNECT BY rownum < 10
             ORDER BY rownum desc)
    LOOP
        v_num := TRUNC(v_running_input/i.num_value,0);
        IF v_num > 0 THEN
            v_amt_in_words := v_amt_in_words||' '||TO_CHAR(TO_DATE(v_num,'J'), 'JSP')||' '||i.place_value;
            v_running_input := v_running_input - (v_num * i.num_value);
        END IF;
    END LOOP;
    v_amt_in_words := v_amt_in_words||' '||TO_CHAR(TO_DATE(TRUNC(v_running_input),'J'), 'JSP')
                      ||' AND '||UPPER(TO_CHAR(TO_DATE((ROUND(v_running_input-TRUNC(v_running_input),2)*100),'J'),'JSP'))||' CENTS';
    RETURN TRIM(v_amt_in_words);
END;
/

要使用它,

SELECT BIG_AMT_IN_WORDS(65763245345658.12) amt_in_words 
  FROM DUAL;

Output
---------------------------------------------
SIXTY-FIVE TRILLION SEVEN HUNDRED SIXTY-THREE BILLION TWO HUNDRED FORTY-FIVE MILLION THREE HUNDRED FORTY-FIVE THOUSAND SIX HUNDRED FIFTY-EIGHT AND TWELVE CENTS

答案 1 :(得分:0)

引发错误,因为您显示的high的值是小数,与100.00不同,它不能隐式转换为整数。因此,它无法转换为朱利安日期。

SELECT  UPPER(TO_CHAR(TO_DATE(100.10,'J'),'JSP'))AMT_IN_WORDS FROM DUAL;

这会导致

  

ORA-01830:日期格式图片在转换整个输入之前结束   串

这可以通过将小数舍入到最接近的整数来解决。

SELECT  UPPER(TO_CHAR(TO_DATE(ROUND(100.10),'J'),'JSP'))AMT_IN_WORDS FROM DUAL;

| AMT_IN_WORDS |
|--------------|
|  ONE HUNDRED |

Demo

如果你真的想要浮动组件,虽然有限,你可以参考这个答案的编辑2:How to convert number to words - ORACLE