Question

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

);
Was it helpful?

Solution

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.

OTHER TIPS

Alex's answer describes the problem very well; there is a simpler solution though, if you're able to change your generated insert statements slightly. TO_TIMESTAMP() has a third parameter, which lets you specify the NLS_DATE_LANGUAGE in the insert itself.

create table a ( tstamp timestamp );

Table created.

insert into a
values (to_timestamp( '20-08-12 01.09.53.271000000 AM'
                    , 'DD-MM-RR HH.MI.SS.FF PM'
                    , 'nls_date_language = ENGLISH'));

1 row created.

select *
  from a;

TSTAMP
------------------------------------------------------
20-AUG-12 01.09.53.271000

You don't then need to alter the date language for the session, or alter it back afterwards.

Without any data to look at I can't be sure but it sound like your "big SQL file" does not always (or sometimes) have the time stamps in the format you are expecting and is not using AM or PM.

Try using a 24-hour clock rather than AM or PM and see if that matches the data in the "big SQL file":

 ...
 to_timestamp('20-AUG-12 01.09.53.271000000','DD-MON-RR HH24.MI.SS.FF'),
 to_timestamp('20-AUG-12 13.09.53.271000000','DD-MON-RR HH24.MI.SS.FF'),
 ...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top