I want a table with the name of an employee and the year of his birth. ONLY THE YEAR IN DATE FORMAT:
CREATE TABLE EMPLOYEE(
Name VARCHAR2(50) NOT NULL,
Year_Birth DATE NOT NULL,
PRIMARY KEY (Name)
);
I want to do this:
INSERT INTO EMPLOYEE(Name, Year_Birth)
VALUES ('John Smith', 1985);
But it doesn't work (cause i'm passing a number value to a date column). I also tried this:
INSERT INTO EMPLOYEE(Name, Year_Birth)
VALUES ('John Smith', to_date('1972','YYYY') )
If i try this one i will get this: ORA-02290: check constraint (PROJECTNAME.SYS_C0066777818) violated
Year_Birth must be a date column. Is there any way to achieve this?
I am assuming that your mysterious check constraint SYS_C0066777818 is enforcing a rule that
year_birthmust be 1st January.to_date('1972','YYYY')does not give the 1st January 1972 as you might expect, it gives the first of the current month, in 1972. Who knows why, but that's the way it works. If you want 1st January 1972 then you will have to specify it explicitly, for example:or an equivalent
to_dateexpression that includes at least the month, ortrunc(somedatevariable,'YEAR').