ORA-01843: not a valid month when inserting a date - sql

I am using Apex Oracle to run a script file merging data/tables with an existing schema. Here is the complete script file.
The error is produced on Every Insert Command stating not a valid month.
One error is produced on Alter command stating Column Type incompatible with referenced column type
The Script File:
--A1_rr_upd.txt file
--dropping the table if already exists
DROP TABLE RRSTAFF;
--creating new table for RRSTAFF
CREATE TABLE RRSTAFF (
staff_num CHAR(4) PRIMARY KEY,
name VARCHAR(20),
gender CHAR(1),
date_join DATE,
date_resign DATE,
contact_num NUMBER(11),
address VARCHAR(255)
);
--Adding new hired staff
INSERT INTO RRSTAFF VALUES ('s001','Adrian','M','2021/07/02',null,'60122000000','6 Jalan BU6, Petaling Jaya,Selangor');
INSERT INTO RRSTAFF VALUES ('s002','Jewel','F','2021/07/12',null,'60123000000','2 Jalan PJS2, Sunway,Selangor');
INSERT INTO RRSTAFF VALUES ('s003','Sean','M','2021/07/12',null,'60166000000','100 Sunway South K,Selangor');
--Adding new customer details
INSERT INTO rrcustomer VALUES ('1011','Dr','Brendan');
INSERT INTO rrcustomer VALUES ('1012','Dr','Haya');
--Adding new records into models
INSERT INTO model VALUES ('LGC83','LG C1 83 in OLED 4K TV', '500');
INSERT INTO model VALUES ('LGG77','LG Gallery 77 in OLED 4K TV', '400');
INSERT INTO model VALUES ('SNY43','Sony 43 in X75 4K Ultra HD Android TV', '200');
INSERT INTO model VALUES ('SHA50','Sharp 50 in Full HD Basic TV ', '80');
--Adding new records into appliance
INSERT INTO appliance VALUES ('2010','LGC83','E',null);
INSERT INTO appliance VALUES ('2011','LGC83','E',null);
--Altering the HIRE table to link it with RRSTAFF using staff_id as Foriegn Key
ALTER TABLE
hire ADD(
staff_id VARCHAR(4),
FOREIGN KEY (staff_id) REFERENCES rrstaff(staff_num)
);
--Adding new hire records
INSERT INTO hire VALUES ('2010','2021/08/02','1011','2021/08/08','s001');
INSERT INTO hire VALUES ('2010','2021/08/22','1012','2021/08/28','s001');
INSERT INTO hire VALUES ('2011','2021/08/12','1013',null,'s001');

Use TO_DATE('2021/07/02', 'YYYY/MM/DD') to convert your date values to the standard database date format
Replace the above date value as your date columns

In Oracle, you express date constants using the date keyword and a string in the ISO standard YYYY-MM-DD format. For instance:
INSERT INTO RRSTAFF
VALUES ('s001', 'Adrian', 'M', DATE '2021-07-02', null, '60122000000', '6 Jalan BU6, Petaling Jaya,Selangor');
Here is a db<>fiddle.

using to_date makes the script robust and independent from an implicit format. It also makes the code more readable since to_date is quite obvious in its syntax.

Related

How to insert a date in format "dd/mm/yyyy" in SQL?

I have the following table:
drop table if exists Activity;
create table Activity (
id_activity serial,
type varchar(255),
day date
primary key (id_activity)
);
I have to insert different values to this table. The issue is that the date format accepts this format by default "yyyy/mm/dd" and I have to insert the date in format "dd/mm/yyyy".
This is what I'm trying:
insert into Activity (type, day) values ("football", [What should I type here?]);
How can I solve this?
Thanks in advance.
try this
insert into Activity (type, day) values ("football", CONVERT(date,
YourDateValue, 103))
You can get the reference here

SQL Beginner trying to insert data on tables

I have started my journey in learning SQL and right I am having trouble creating and inserting data into tables. Here is the code that I have tried, I get an error message saying that there aren't enough values. I am using Oracle.
Create table project
(
proj_id number(10),
medic_name varchar2(10),
purpose varchar2(12),
start_date date,
end_date date,
pi_id null,
CONSTRAINT pkprojid primary key (proj_id),
CONSTRAINT fkproject foreign key (pi_id) references researcher
);
alter session set nls_date_format = 'mm/dd/yyyy';
Insert into project values (PR001, 'Medic1', 'heart', '09/01/2017', '07/31/2019');
Insert into project values (PR002, 'Medic1', 'diabetes', '10/01/2016', '07/31/2020);
Insert into project values (PR003, 'Medic3', 'lung', '11/1/2014', '12/31/2020');
Insert into project values (PR004, 'Medic3', 'blood', '01/10/2017', '07/31/2019');
Insert into project values (PR005, 'Medic5', 'blood', '07/10/2018', '01/31/2020');
alter session set nls_date_format = 'mm/dd/yyyy';
Insert into project values (PR001, 'Medic1', 'heart', '09/01/2017', '07/31/2019');
Issues:
Your table has 6 columns, you are only passing 5 for insert; it seems like you are missing last column (pi_id), hence the error message that you are getting. If you want to skip the last column (which is possible since it is declared as nullable), you can explictly list the column when inserting
first column (proj_id) is of number datatype; PR001 is not a number (neither a string, since it is not quoted: this is a syntax error); did you mean 1 instead? Or, if you want to insert string values, you need to change the datatype of column proj_id to varchar(N) (N being the maximum length of the string, in bytes).
Here is an insert statement that should work for your current table definition:
insert into project(proj_id, medic_name, purpose, start_date, end_date)
values (1, 'Medic1', 'heart', '09/01/2017', '07/31/2019');
Note: there is a missing quote at the end of the date on the second insert statement; I assume that this is a typo.

Unable to create or query tables in online sql interpreter

I've been trying to simply create and display information from these tables on an interpreter that uses sql.js to run.
I've looked through assignment forums and tried to assign primary keys in varying formats based on what was provided on w3schools and also tried to explicitly create a database to put the tables into. no changes.
DROP TABLE IF EXISTS employees;
CREATE TABLE pledges( donorId integer , donor text,
pledge integer, AmountPaid integer,
);
INSERT INTO pledges VALUES (1,'JOHNSON',6,30);
INSERT INTO pledges VALUES (2,'ROGERS',5,100);
INSERT INTO pledges VALUES (1,'RODDUCK',10,50);
INSERT INTO pledges VALUES (1,'PETERS',2,20);
INSERT INTO pledges VALUES (1,'ALBERTSON',7,56);
SELECT * FROM pledges;
The expectation is just for me to create the simple tables and test the queries but it just keeps saying "fetching results"
Your online tool probably swallows the error due to the incorrect CREATE TABLE statement. You have a dangling , after the amountpaid integer definition:
Once that error is fixed (and the pledges table is dropped instead of the employees table), your script runs fine:
DROP TABLE IF EXISTS pledges;
CREATE TABLE pledges
(
donorId integer,
donor text,
pledge integer,
amountpaid integer --<< you had a comma here
);
INSERT INTO pledges VALUES (1,'JOHNSON',6,30);
INSERT INTO pledges VALUES (2,'ROGERS',5,100);
INSERT INTO pledges VALUES (1,'RODDUCK',10,50);
INSERT INTO pledges VALUES (1,'PETERS',2,20);
INSERT INTO pledges VALUES (1,'ALBERTSON',7,56);
SELECT *
FROM pledges;
https://rextester.com/BJJGC94351

SQL data truncation for date value

I'm having a hard time creating a simple table:
CREATE TABLE `csat` (
`csat_id` INT NOT NULL AUTO_INCREMENT,
`value` INT,
`month` DATE NOT NULL,
PRIMARY KEY (`csat_id`)
);
CREATE TABLE `migrated` (
`migrated_id` INT NOT NULL AUTO_INCREMENT,
`title` INT,
`description` INT,
`month` DATE NOT NULL,
PRIMARY KEY (`migrated_id`)
);
INSERT INTO csat
VALUES (1, 1, 2017-06-15);
INSERT INTO migrated
VALUES (1, 2, 2018-06-15);
I get the error:
Data truncation: Incorrect date value: '1996' for column 'month' at row 1
It seems like my date is in the right format:
https://www.w3schools.com/sql/func_mysql_date.asp
I'm also wondering why I need to specify a value on the csat_id, because I thought SQL would just put that in for me since its the primary key.
You have to wrap your date values in single quotation marks: '2017-06-15', not 2017-06-15. Right now, MySQL is evaluating this as 2017 minus 6 minus 15, which comes to 1996.
Also, when inserting, it's best to specify the columns you're inserting into. And if your column is set to AUTO_INCREMENT, you don't need to specify it:
INSERT INTO csat
(`value`, `month`)
VALUES
(1, '2017-06-15');
I would also consider changing your column names. Perhaps make "value" more descriptive (value of what?) And month is misleading, since it's actually a date-type column.
You haven't said which database server you're using, but generally speaking dates are inputted as strings.
You should try the following inserts;
INSERT INTO csat (`csat_id`, `value`, `month`)
VALUES (1, 1, '2017-06-15');
INSERT INTO migrated (`migrated_id`, `title`, `description`, `month`)
VALUES (1, 2, 2, '2018-06-15');
Also, you should specify which columns you're inserting into. This prevents data from being entered into the wrong fields, especially when schema changes occur.
SQL does auto increment primary key fields (if defined that way). However, you had to define it in your insert statements because you didn't specify the columns you were inserting to.
Try this instead;
INSERT INTO csat (`value`, `month`)
VALUES (1, '2017-06-15');
INSERT INTO migrated (`title`, `description`, `month`)
VALUES (2, 2, '2018-06-15');
I guess you missed the single qoutes (as per Sql standards) at first in your date and then while inserting even if the column is autoincrement you need to specify columns other than the autoincrement column so as to make sure the data you are inserting belongs to that specific column or not
Try this
INSERT INTO
csat(value,month) values
(1,'2017-06-15')

SQL: Add only a year value in a date column

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 think birth year should be an INT, like this:
CREATE TABLE EMPLOYEE(
Name VARCHAR(50) NOT NULL,
Year_Birth INT NOT NULL,
PRIMARY KEY (Name)
);
Then this should work:
INSERT INTO EMPLOYEE(Name, Year_Birth)
VALUES ('John Smith', 1985);
You can create a column which is of data type int. This will then let you save the year of birth.
If the column must be of data type date then you could just save the date as the first of Jan with the relevant year (eg. for the year 2017 enter '20170101'). This will still allow you then perform date calculations on the data.
You can create a column which is of data type int. The insert statement will work.
insert into employee(Name, Year_Birth)
values ('John Smith', 1985);
I am assuming that your mysterious check constraint SYS_C0066777818 is enforcing a rule that year_birth must 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:
insert into employee (name, year_birth)
values ('John Smith', date '1972-01-01');
or an equivalent to_date expression that includes at least the month, or trunc(somedatevariable,'YEAR').