Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ORA-01843 not a valid month- Comparing Dates

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  
like image 620
Davidin073 Avatar asked Jan 16 '14 08:01

Davidin073


People also ask

How do I fix not valid month error in Oracle?

To fix the error, specify a month value that is valid such as “January”. Some conversion is built-in, and a value of “Jan” is also valid for the Month format code.

How do I fix a not valid month error in SQL?

Fix: To fix this, update your SQL statement to remove the mistake and use the correct month value. SELECT TO_DATE('01-JAN-2015') FROM dual; If the value is correct, and you're still getting the error, it could be to do with the format you've entered. TO_DATE allows you to enter a format along with the input value.

What is DD Mon RR format?

RR is an "input" format. it means if you enter to_date( '01-jan-40', 'dd-mon-rr' ) Oracle will slide around the date based on the current year. In 1999 and 2001 -- that would be the year 2040. As opposed to yy -- where the century is based on the current date.

What is the default date format in Oracle?

Oracle stores dates in an internal numeric format representing the century, year, month, day, hours, minutes, seconds. The default date format is DD-MON-YY. SYSDATE is a function returning date and time. DUAL is a dummy table used to view SYSDATE.


2 Answers

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'); 
like image 103
Thomas B. Lze Avatar answered Oct 08 '22 03:10

Thomas B. Lze


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'); 
like image 23
Mureinik Avatar answered Oct 08 '22 01:10

Mureinik