ORA-01843 not a valid month- Comparing Dates

前端 未结 9 2219
南方客
南方客 2020-12-15 03:49

I have a problem when try to select data from a table filtering by date.

For example:

SELECT * FROM MYTABLE WHERE MYTABLE.DATEIN = \'23         


        
相关标签:
9条回答
  • 2020-12-15 04:28

    Just in case this helps, I solved this by checking the server date format:

    SELECT * FROM nls_session_parameters WHERE parameter = 'NLS_DATE_FORMAT';
    

    then by using the following comparison (the left field is a date+time):

    AND EV_DTTM >= ('01-DEC-16')
    

    I was trying this with TO_DATE but kept getting an error. But when I matched my string with the NLS_DATE_FORMAT and removed TO_DATE, it worked...

    0 讨论(0)
  • 2020-12-15 04:33

    You are comparing a date column to a string literal. In such a case, Oracle attempts to convert your literal to a date, using the default date format. It's a bad practice to rely on such a behavior, as this default may change if the DBA changes some configuration, Oracle breaks something in a future revision, etc.

    Instead, you should always explicitly convert your literal to a date and state the format you're using:

    SELECT * FROM MYTABLE WHERE MYTABLE.DATEIN = TO_DATE('23/04/49','MM/DD/YY');
    
    0 讨论(0)
  • 2020-12-15 04:36

    I know this is a bit late, but I'm having a similar issue. SQL*Plus executes the query successfully, but Oracle SQL Developer shows the ORA-01843: not a valid month error.

    SQL*Plus seems to know that the date I'm using is in the valid format, whereas Oracle SQL Developer needs to be told explicitly what format my date is in.

    • SQL*Plus statement:

      select count(*) from some_table where DATE_TIME_CREATED < '09-12-23';
      

    VS

    • Oracle SQL Developer statement:

       select count(*) from some_table where DATE_TIME_CREATED < TO_DATE('09-12-23','RR-MM-DD');
      
    0 讨论(0)
  • 2020-12-15 04:42

    If you don't need to check exact timestamp, use

    SELECT * FROM MYTABLE WHERE trunc(DATEIN) = TO_DATE('23-04-49','DD-MM-YY');
    

    otherwise, you can use

    SELECT * FROM MYTABLE WHERE DATEIN = TO_DATE('23-04-49 20:18:07','DD-MM-YY HH24:MI:SS');
    

    Here, you use hard code date,if you directly compare then you must use DD-MM-YY HH24:MI:SS else you might get ORA-01849: hour must be between 1 and 12.

    0 讨论(0)
  • ALTER session set NLS_LANGUAGE=’AMERICAN’;

    0 讨论(0)
  • 2020-12-15 04:44

    If you are using command line tools, then you can also set it in the shell.

    On linux, with a sh type shell, you can do for example:

    export NLS_TIMESTAMP_FORMAT='DD/MON/RR HH24:MI:SSXFF'
    

    Then you can use the command line tools and it will use the specified format:

    /path/to/dbhome_1/bin/sqlldr user/pass@host:port/service control=table.ctl direct=true
    
    0 讨论(0)
提交回复
热议问题