简体   繁体   中英

format date in oracle SQL

I was trying to learn how to format date in oracle pl oracle, when I ran below query its returns error

SELECT TO_DATE('01-JAN-00', 'YYYY-DD-MM') FROM dual;

the error message is

ORA-01858: a non-numeric character was found where a numeric was expected
01858. 00000 -  "a non-numeric character was found where a numeric was expected"
*Cause:    The input data to be converted using a date format model was
           incorrect.  The input data did not contain a number where a number was
           required by the format model.
*Action:   Fix the input data or the date format model to make sure the
           elements match in number and type.  Then retry the operation.

You are either not using the correct format specifier, or not passing the correct string. You want:

SELECT TO_DATE('2000-01-01', 'YYYY-DD-MM') FROM DUAL;

Or:

SELECT TO_DATE('01-JAN-00', 'DD-MON-YY') FROM DUAL;

Or you can simply declare a DATE litteral:

SELECT DATE'2000-01-01' FROM DUAL;

对于我的场景,我不得不使用 to_char 完美解决格式问题。

SELECT TO_CHAR('01-JAN-00', 'yyyy-DD-MM') FROM dual;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM