the following code gives me this error message - "ORA-01855: AM/AM or PM/PM required"
SELECT TO_DATE
('2/2/2021 8:08:58 PM','MM/DD/YYYY HH:MI:SS AM') "NOW"
FROM DUAL;
If I try to convert to char instead, I get "ORA-01722: invalid number".
SELECT TO_CHAR('2/2/2021 8:08:58 PM', 'MM/DD/YYYY HH:MI:SS AM') "NOW"
FROM DUAL;
I don't have much experience with oracle database and have been struggling with it for a while. Not sure what I'm doing wrong. Thanks for any help!
It looks like you probably have NLS_DATE_LANGUAGE
set to something other than English; but not NLS_LANGUAGE
as that would affect the error message too:
alter session set nls_language = 'CZECH';
SELECT TO_DATE
('2/2/2021 8:08:58 PM','MM/DD/YYYY HH:MI:SS AM') "NOW"
FROM DUAL;
ORA-01855: požaduje se AM/A.M. nebo PM/P.M.
With just the date language changed:
alter session set nls_language = 'ENGLISH';
alter session set nls_date_language = 'CZECH';
SELECT TO_DATE
('2/2/2021 8:08:58 PM','MM/DD/YYYY HH:MI:SS AM') "NOW"
FROM DUAL;
ORA-01855: AM/A.M. or PM/P.M. required
You can either change the session setting to English, or change the string to have the language-appropriate AM/PM indicator value:
alter session set nls_date_language = 'CZECH';
SELECT TO_DATE
('2/2/2021 8:08:58 ODPOLEDNE','MM/DD/YYYY HH:MI:SS AM') "NOW"
FROM DUAL;
NOW
---------
02-ÚNO-21
Or probably most practically and simply, override the session setting in the query:
alter session set nls_date_language = 'CZECH';
SELECT TO_DATE
('2/2/2021 8:08:58 PM','MM/DD/YYYY HH:MI:SS AM','NLS_DATE_LANGUAGE=ENGLISH') "NOW"
FROM DUAL;
NOW
---------
02-ÚNO-21
You should use TO_CHAR to convert from date to char, not from char:
SELECT TO_CHAR(TO_DATE('2/2/2021 8:08:58 PM', 'MM/DD/YYYY HH:MI:SS AM'),
'MM/DD/YYYY HH:MI:SS AM') "NOW"
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.