Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL "not a valid month"

Tags:

sql

oracle

I have a table in sql as follows:

CREATE TABLE Reserves(
    sid INTEGER,
    bid INTEGER,
    day DATE,
    PRIMARY KEY (sid, bid, day),
    FOREIGN KEY (sid) REFERENCES Sailors,
    FOREIGN KEY (bid) REFERENCES Boats
);

and I'm trying to insert into it:

INSERT INTO Reserves VALUES(22, 101, '01-01-1998');

But I get the error: ORA-01843: not a valid month

This is an Oracle db. I'm not sure what's wrong with my date format.

like image 530
Matt Avatar asked Oct 21 '12 21:10

Matt


People also ask

How do I resolve Ora 01843 Not a valid month error?

It may be best to find the specific point of the code and correct the syntax of the month if this is not a frequent occurrence. ALTER session set NLS_DATE_FORMAT='DD/MM/YYYY'; To avoid seeing error ORA-01843, be sure to write valid values for months.

What does Add_months do in SQL?

ADD_MONTHS returns the date date plus integer months. A month is defined by the session parameter NLS_CALENDAR . The date argument can be a datetime value or any value that can be implicitly converted to DATE . The integer argument can be an integer or any value that can be implicitly converted to an integer.

How do you fix Ora 01858 a non numeric character was found where a numeric was expected?

The Solution In order to solve this formatting issue, there are basically two options to take. The user can either fix the input data to conform to the format in question, or the user can switch the date format model to ensure that the elements match in number and type and then retry the operation.

What is Nls_date_format?

NLS_DATE_FORMAT specifies the default date format to use with the TO_CHAR and TO_DATE functions. The default value of this parameter is determined by NLS_TERRITORY . The value of this parameter can be any valid date format mask, and the value must be surrounded by double quotation marks.


3 Answers

As @Jody also mentioned,
You can change the default for your session by executing this code once before INSERT :

ALTER SESSION SET NLS_DATE_FORMAT = 'DD-MM-YYYY';

You may alter the format in any order you like.

Source: dba-oracle.com

like image 181
Hamed Avatar answered Oct 04 '22 11:10

Hamed


It's not entirely clear which you wanted, so you could try:

  • For month-day-year format:

    INSERT INTO Reserves VALUES(22, 101, TO_DATE('01-01-1998','MM-DD-YYYY'));

  • For day-month-year format:

    INSERT INTO Reserves VALUES(22, 101, TO_DATE('01-01-1998','DD-MM-YYYY'));

Also, recommended reading: Oracle functions: TO_DATE

like image 23
ppeterka Avatar answered Oct 04 '22 11:10

ppeterka


You can use the date keyword to specify an ANSI-standard date string:

INSERT INTO Reserves VALUES(22, 101, date '1998-01-01');

In this case, the format is YYYY-MM-DD, or January 1, 1998.

like image 28
BellevueBob Avatar answered Oct 04 '22 09:10

BellevueBob