select TRUNC(TO_DATE('22-AUG-03'), 'YEAR')
from dual;
ORA-01843: not a valid month
1st example -> https://www.techonthenet.com/oracle/functions/trunc_date.php
I know "trunc" function takes in a date and optional fmt parameter. Why am I getting this error?
I don't think the problem is trunc()
. I think the problem is the date format. You are safer using the date
keyword and an ISO-standard formatted date:
select TRUNC(DATE '2003-08-22', 'YEAR')
from dual;
The interpretation of a date string depends on the internationalization settings for your particular environment. The above does not have that dependency.
Here is a very simple check: IN THE SAME SESSION where your SELECT
fails, and WITHOUT you altering the NLS_DATE_FORMAT
, see what happens if you run the simpler statement,
SELECT TO_DATE('22-AUG-03') FROM DUAL
You will get the same error - which proves conclusively that it has nothing to do with TRUNC()
.
To make everything work exactly as in the tutorial, issue the command
ALTER SESSION SET NLS_DATE_FORMAT = 'dd-MON-yy'
first, before everything else. Note though that formats that have the year as two digits instead of four are a very bad idea in most cases.
似乎您想获取参数中收到的日期的年份。如果您将'22 -AUG-03'作为参数,则将所需的日期格式添加到TO_DATE函数中,然后将其截断:
SELECT TRUNC(TO_DATE('22-AUG-03', 'dd-MON-yy'),'YEAR') FROM DUAL
Use SELECT EXTRACT (YEAR FROM TO_DATE('22-AUG-03', 'dd-MON-yy')) FROM DUAL; to extract year from date.
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.