Encountering SQL Error: ORA-01843: not a valid month

前端 未结 5 985
心在旅途
心在旅途 2021-01-21 21:33

I created a table using this query
CREATE TABLE Store (id number(11) primary key not null, opening_time timestamp CHECK (EXTRACT(HOUR FROM opening_time) > 8 || NUL

相关标签:
5条回答
  • 2021-01-21 21:52

    '04/04/2012 13:35 PM' is not a date - it is a string.

    Oracle will do an implicit TO_DATE( string_value, format_mask ) on non-date literals when inserting them into a DATE column using the value of the NLS_DATE_FORMAT session parameter as the format mask (note: this is a session parameter and belongs to the client; it is not a global setting). If the non-date literal matches this format then it will work (and if it doesn't then it won't) - however, if the NLS_DATE_FORMAT is ever changed then it will immediately break (any be a huge pain to debug as the code that was working won't but no-one will have changed the code).

    You can find out your current NLS_DATE_FORMAT with the query:

    SELECT VALUE
    FROM   NLS_SESSION_PARAMETERS
    WHERE  PARAMETER = 'NLS_DATE_FORMAT';
    

    It is better to explicitly use TO_DATE() with the correct format mask or to use an ANSI/ISO date literal (i.e. DATE '2012-04-04' or TIMESTAMP '2012-04-04 13:35').

    You can do:

    INSERT INTO STORE ( id, opening_time )
      VALUES( 1, TO_DATE( '04/04/2012 13:35', 'DD/MM/YYYY HH24:MI' );
    

    (you do not need the AM/PM as the hour component is already on a 24 hour clock)

    or

    INSERT INTO STORE ( id, opening_time )
      VALUES( 1, TIMESTAMP '2012-04-04 13:35:00' );
    

    (using the ANSI/ISO timestamp literal which Oracle will implicitly convert to a date)

    0 讨论(0)
  • 2021-01-21 21:58

    insert into Store values(1, '04-APR-2012 13:35 PM');

    This works for me.

    0 讨论(0)
  • 2021-01-21 22:08

    SQL Error: ORA-01843: not a valid month

    '04/04/2012 13:35 PM' is a string and not a date. You should always use TO_DATE to explicitly convert a string into date. Never ever rely on implicit datatype conversion. You might just be lucky to depend on your locale-specific NLS settings. However, it won't work if the NLS settings are different. So, always use TO_DATE to convert a literal into date using proper format mask.

    Looking at the datetime value, no need to use timestamp, simply use DATE data type. It can hold till precision up to seconds.

    CREATE TABLE Store (id number(11) primary key not null, 
                        opening_time timestamp 
                        CHECK (EXTRACT(HOUR FROM opening_time) > 8 || NULL)); 
    
    insert into Store 
    values(1, TO_DATE('04/04/2012 13:35 PM', 'DD/MM/YYYY HH:MI', 'nls_date_language=ENGLISH'));
    

    You could also use the ANSI date/timestamp literal which uses a fixed format and therefore it is NLS independent.

    DATE '2012-04-04' 
    

    or

    TIMESTAMP '2012-04-04 13:35:00'
    
    0 讨论(0)
  • 2021-01-21 22:08

    Good practice is using to_date function with your data format. In you case it will:

    insert into Store 
    values(1, to_date('04/04/2012 13:35', 'DD/MM/YYYY HH24:mi'));
    

    More details about this function you can found here in official documentation.

    0 讨论(0)
  • 2021-01-21 22:17

    You dont have to insert the primary key value in table. It is auto generated. And try to provide the format of your date using the TO_DATE function like:

    insert into Store values(to_date('04/04/2012 13:35 PM','DD/MM/YYYY HH:MI AM'));
    
    0 讨论(0)
提交回复
热议问题