10

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.

1
  • 4
    As a general rule you should always use a culture inspecific date format (yyyyMMdd) unless you are declaring the date format explcitly. 02-01-2012 could be 2nd January 2012 or 1st February 2012, however 20120102 is always 2nd January 2012 Commented Oct 21, 2012 at 22:01

6 Answers 6

27

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

Sign up to request clarification or add additional context in comments.

Comments

12

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.

1 Comment

Thanks Bob. I was hoping there was a way that I wouldn't have to use the TO_DATE function.
8

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

Comments

1

Try '1998-01-01'.

I believe the default date format for oracle is yyyy-mm-dd. You can change the default for your session by using alter session set nls_date_format='mm-dd-yyyy'

Keep in mind that most clients let you set this to whatever you like permanently

Comments

1

Also you can try in below query format:

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

DATE keyword interprets the following string as a date.

Comments

0

Try this it will solve your issue

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

1 Comment

How is this different than the accepted answer?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.