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

前端 未结 5 989
心在旅途
心在旅途 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条回答
  •  猫巷女王i
    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'
    

提交回复
热议问题