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.
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.
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.
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.
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.
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
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With