42

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

For example:

SELECT * FROM MYTABLE WHERE MYTABLE.DATEIN = '23/04/49';

The Oracle Error is:

Informe de error:
Error SQL: ORA-01843: mes no válido
01843. 00000 -  "not a valid month"
*Cause:    
*Action:

Probably the source data of table is corrupted, in this case:

  • How can i solve this problem?
  • Can I change this dates for null?

The results of this select, select * from nls_session_parameters; , is:

PARAMETER                      VALUE                                  
------------------------------ ----------------------------------------
NLS_LANGUAGE                   SPANISH                                  
NLS_TERRITORY                  SPAIN                                    
NLS_CURRENCY                   ¿                                        
NLS_ISO_CURRENCY               SPAIN                                    
NLS_NUMERIC_CHARACTERS         ,.                                       
NLS_CALENDAR                   GREGORIAN                                
NLS_DATE_FORMAT                DD/MM/RR                                 
NLS_DATE_LANGUAGE              SPANISH                                  
NLS_SORT                       SPANISH                                  
NLS_TIME_FORMAT                HH24:MI:SSXFF                            
NLS_TIMESTAMP_FORMAT           DD/MM/RR HH24:MI:SSXFF                   
NLS_TIME_TZ_FORMAT             HH24:MI:SSXFF TZR                        
NLS_TIMESTAMP_TZ_FORMAT        DD/MM/RR HH24:MI:SSXFF TZR               
NLS_DUAL_CURRENCY              ¿                                        
NLS_COMP                       BINARY                                   
NLS_LENGTH_SEMANTICS           BYTE                                     
NLS_NCHAR_CONV_EXCP            FALSE 
2
  • The used format for date seems to be US format : 'month/day/year' so month 23 is wrong. Try with '04/23/49' Commented Jan 16, 2014 at 8:57
  • The format of date is 'DD/MM/YY', is the same if try to change the format for 'MM/DD/YY'.
    – Davidin073
    Commented Jan 16, 2014 at 8:59

14 Answers 14

58

You should use the to_date function (oracle/functions/to_date.php )

SELECT * FROM MYTABLE WHERE MYTABLE.DATEIN = TO_DATE('23/04/49', 'DD/MM/YY');
8
  • 7
    I would say: should use. When comparing dates always use formatting. Otherwise it will go wrong on some system somewhere.
    – Rene
    Commented Jan 16, 2014 at 9:03
  • 1
    Yes Rene. But is the same with or wothout format, with TO_DATE function or without it. The error is the same --> 01843. 00000 - "not a valid month".
    – Davidin073
    Commented Jan 16, 2014 at 9:05
  • Is the column you are using "mytable.datein" of type date?
    – Rene
    Commented Jan 16, 2014 at 10:11
  • Yes, the column type is DATE and is NULLABLE.
    – Davidin073
    Commented Jan 16, 2014 at 10:25
  • Then please post the statement with to_date and that still gives you an exception.
    – Rene
    Commented Jan 16, 2014 at 10:46
14

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');
2
  • Of course Mureinik, but if you have a table with this date : 23/04/49 and try to search with previous select, do you get the same error?
    – Davidin073
    Commented Jan 16, 2014 at 9:12
  • 1
    @Davidin073 The date is NOT stored as 23/04/49. This is just a string representation of it. Commented Aug 3, 2021 at 16:44
10

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.

4

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');
    
2

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

In a comment to one of the answers you mention that to_date with a format doesn't help. In another comment you explain that the table is accessed via DBLINK.

So obviously the other system contains an invalid date that Oracle cannot accept. Fix this in the other dbms (or whatever you dblink to) and your query will work.

Having said this, I agree with the others: always use to_date with a format to convert a string literal to a date. Also never use only two digits for a year. For example '23/04/49' means 2049 in your system (format RR), but it confuses the reader (as you see from the answers suggesting a format with YY).

3
  • I cant acces to source table. So its impossible modify this data. Its irrelevant data of year because the error its in the month.
    – Davidin073
    Commented Jan 16, 2014 at 11:21
  • No, maybe you can't. But you may be able to report this, so somebody can fix this in the other system. As to the year: It has nothing to do with the error. It is just a general recommendation. Commented Jan 16, 2014 at 11:23
  • Ok, so i,ll report this error. Thanks for your recommendation, Thorsten, but its normal have to work with code created previouly by other persons and cant modify some implementations.
    – Davidin073
    Commented Jan 16, 2014 at 11:27
0

If the source date contains minutes and seconds part, your date comparison will fail. you need to convert source date to the required format using to_char and the target date also.

0

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

Try using:

SELECT *
FROM MYTABLE
WHERE MYTABLE.DATEIN is not null 
AND MYTABLE.DATEIN = '23/04/49';
0
0

Use the month as a string.

Example:

(12-Apr-2002) or (12-April-2002)
0
0

Although the answers using TO_DATE are correct, I prefer to use the ANSI SQL format for dates:

DATEIN = DATE '1949-04-23'

It works in Oracle and other DBMS ANSI SQL compliant. This is specially important if your application is DBMS agnostic.

0

Try alter session set NLS_DATE_FORMAT='DD/MM/YY'; -- or whatever format you want

I faced the same problem, on PROD, all the code were already in this format, but on preprod, it's not set, So this means you change the default date format used by oracle

0

I added an "AND ROWNUM < 1000000" to where clause and it run. (!)

1
-1

ALTER session set NLS_LANGUAGE=’AMERICAN’;

2
  • 1
    thanks for your answer but could you please include a little context? Explain why this solves the problem if you can.
    – Dan
    Commented Apr 13, 2020 at 21:20
  • He was being facetious, and poking fun that the SQL environment is set to Español. Commented Oct 8, 2020 at 22:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.