Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle insert timestamp - AM/A.M. or PM/P.M. required

In SQL Developer I have created an export, .sql file, (a really big one). It contains hundreds of inserts like :

"Insert into SCHEMA.TABLE (
 ... ,
 CREATEDATE,
 MODIFIEDDATE,
 ....
) 
values (
 ... , 
 to_timestamp('20-AUG-12 01.09.53.271000000 AM','DD-MON-RR HH.MI.SS.FF AM'),
 to_timestamp('20-AUG-12 01.09.53.271000000 PM','DD-MON-RR HH.MI.SS.FF PM'),
 ...
);"

When I run it in SQL Developer, it works fine. But I can't import a bit .sql file with SQL Developer (or I don't know how to do it).

When I run it with SQL*Plus I get following error:

ORA-01855: AM/A.M. or PM/P.M. required

When I remove the AM/PM keywords from the insert, SQL*Plus can insert row, with them, no luck. What is the problem with AM / PM?

Here is the exact insert command:

Insert into SCHEMA.TABLE(

ENTRYID,GROUPID,COMPANYID,USERID,USERNAME,CREATEDATE,MODIFIEDDATE,
CLASSNAMEID,CLASSPK,CLASSUUID,VISIBLE,STARTDATE,ENDDATE,PUBLISHDATE,
EXPIRATIONDATE,MIMETYPE,TITLE,DESCRIPTION,SUMMARY,URL,HEIGHT,WIDTH,
PRIORITY,VIEWCOUNT

) values (

11902,11005,10136,10178,'Test Test',
to_timestamp('20-AUG-12 01.09.53.271000000 AM','DD-MON-RR HH.MI.SS.FF AM'),
to_timestamp('20-AUG-12 01.09.53.271000000 PM','DD-MON-RR HH.MI.SS.FF PM'),
10076,11900,'AAA',1,null,null,null,null,null,
'ABC','XyZ',null,null,0,0,0,2563

);
like image 897
Mato.Duris Avatar asked Dec 20 '13 09:12

Mato.Duris


People also ask

What is the format of TIMESTAMP in Oracle?

Oracle introduced TIMESTAMP data type in 9i version. It allows to store date time with fraction of seconds. By default format of TIMESTAMP is 'YYYY-MM-DD HH24:MI:SS. FF'.

How do I add a TIMESTAMP in Oracle?

You can use the below code: insert into tablename (timestamp_value) values (TO_TIMESTAMP(:ts_val, 'YYYY-MM-DD HH24:MI:SS')); If you need the current timestamp to be inserted then use the following code: insert into tablename (timestamp_value) values (CURRENT_TIMESTAMP);

Does Oracle TIMESTAMP have timezone?

Oracle converts the data to a TIMESTAMP WITH LOCAL TIME ZONE value. This means the time zone that is entered ( -08:00 ) is converted to the session time zone value ( -07:00 ).

Can we insert TIMESTAMP in date column in Oracle?

insert into table x1 select to_timestamp(activity_date,'mm/dd/yyyy hh24:MI:SS') as v_date from csct. products where rownum<=10 . It able to loads the data but without timestamp.


1 Answers

The only immediate way I can see to reproduce this is to change the NLS setings; but not NLS_LANGUAGE as that would affect the error message as well. Using English is OK as you'd expect:

SQL> alter session set NLS_DATE_LANGUAGE='ENGLISH';

Session altered.

SQL> select value from nls_session_parameters
  2  where parameter = 'NLS_DATE_LANGUAGE';

VALUE
--------------------------------------------------------------------------------
ENGLISH

SQL> insert into t42 (createddate)
  2  values (to_timestamp('20-08-12 01.09.53.271000000 AM','DD-MM-RR HH.MI.SS.FF PM'));

1 row created.

Changing just the NLS_DATE_LANGUAGE reproduces your error:

alter session set NLS_DATE_LANGUAGE='SLOVAK';

Session altered.

SQL> insert into t42 (createddate)
  2  values (to_timestamp('20-AUG-12 01.09.53.271000000 AM','DD-MON-RR HH.MI.SS.FF PM'));
values (to_timestamp('20-AUG-12 01.09.53.271000000 AM','DD-MON-RR HH.MI.SS.FF PM'))
                     *
ERROR at line 2:
ORA-01855: AM/A.M. or PM/P.M. required

Changing NLS_LANGUAGE, directly or through something that influences it, would make the error message appear differently as well:

SQL> alter session set NLS_LANGUAGE = 'SLOVAK';

Relácia zmenená.

SQL> insert into t42 (createddate)
  2  values (to_timestamp('20-AUG-12 01.09.53.271000000 AM','DD-MON-RR HH.MI.SS.FF PM'));
values (to_timestamp('20-AUG-12 01.09.53.271000000 AM','DD-MON-RR HH.MI.SS.FF PM'))
                     *
ERROR v riadku 2:
ORA-01855: vy¿aduje sa AM/A.M. alebo PM/P.M.

You'd need a language that still recognises AUG as a valid month, but doesn't use AM/PM; Slovak seems to fit the bill, so I'd guess that might be where you're from. Other languages may do the same too. Bit strange that the error message when NLS_LANGUAGE is still set shows AM/PM, to but to_char(sysdate, 'HH AM') indicates that message is wrong. With only NLS_DATE_LANGUAGE set the message is in English anyway (try it with Turkish, for example, but you'd have to change the month name or switch use month numbers) and it always shows AM/PM.

If that is what's happening, either change the NLS_DATE_LANGUAGE to English before running this script; or change your SQL Developer settings to use Slovak (or whatever you actually use) before doing the export - which would hopefully make the export use DOPOLUDNIE/POPOLUDNIE, though it wouldn't entirely surprise me if it didn't; or change the SQL Developer NLS_TIMESTAMP_FORMAT to use 24-hour time (and a 4-digit year, and month numbers instead of names) for the export, which would avoid the issue entirely. Or run the script from SQL Developer, of course.

like image 53
Alex Poole Avatar answered Sep 23 '22 01:09

Alex Poole