SQLite allows insert of string data into datetime column - sql

Table:
CREATE TABLE logaction
(
actype varchar(8) not null,
actime DATETIME not null,
devid int not null
);
SQL:
insert into logaction values ('TEST', '2013-08-22', 1);
insert into logaction values ('TEST', '2013-08-22 09:45:30', 1);
insert into logaction values ('TEST', 'xyz', 2); // shouldn't this fail?
The last record does make it into the table regardless of the non-datetime value for the actime column. Why and how can I enforce that only good data go in?
Here is a SQL Fiddle.

Well, there's just no DATETIME type in Sqlite...
SQLite does not have a storage class set aside for storing dates
and/or times. Instead, the built-in Date And Time Functions of SQLite
are capable of storing dates and times as TEXT, REAL, or INTEGER
values:
TEXT as ISO8601 strings ("YYYY-MM-DD HH:MM:SS.SSS").
REAL as Julian day numbers, the number of days since noon in Greenwich
on November 24, 4714 B.C. according to the proleptic Gregorian calendar.
INTEGER as Unix Time, the number of seconds since 1970-01-01 00:00:00 UTC.
Applications can chose to store dates and times in any of these
formats and freely convert between formats using the built-in date and
time functions.
see doc
You could have created your table like that, it wouldn't have changed anything.
CREATE TABLE logaction
(
actype varchar(8) not null,
actime NonexistingType not null,
devid int not null
);

Unfortunately, not really in sqlite - it doesn't have a datetime type - see section 1.2 in here

Here's a simple work around:
CREATE TABLE logaction
(
actype varchar(8) not null,
actime DATETIME not null check( DATETIME(actime) is not null ),
devid int not null
);
sqlite> insert into logaction values ('TEST', '2013-08-22', 1);
sqlite> insert into logaction values ('TEST', '2013-08-22 09:45:30', 1);
sqlite> insert into logaction values ('TEST', 'xyz', 2);
Error: constraint failed
sqlite>

Related

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

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.

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')

Can you insert null value into date datatype in SQL (and if yes, how)?

I have been working on my SQL database for school project and I have a problem with my idea. I am doing a car service database (all made up) and I have a column deadline of datatype date for a table order (names for both in my language - 'rok' and 'narocilo') and I am trying to insert null into some of rows where deadline isn't specified by the customer.
create table narocilo
(
id_narocila integer NOT null,
opis varchar(50),
cena integer,
status varchar(20),
datum_narocila date,
rok date
);
This is some ways i tried to insert null but none of them worked
to_date('dd-mm-yyyy', null)
to_date(null)
null
to_date('null')
So, my question is, can I insert null into a date column and if yes, how?
Thank you in advance!
You have to insert an entire row's worth of data, but as long as you have not defined that field as NOT NULL like you have above, it should be fine to insert NULL values into the date fields:
INSERT INTO narocilo(id_narocila, opis, cena, status, datm_narocila, rok)
VALUES (1001, 'something', 6, 'Open', NULL, NULL)
A very simple way to insert NULL is to leave them out of the insert altogether:
INSERT INTO narocilo (id_narocila, opis, cena, status)
VALUES (?, ?, ?, ?);
Technically, this inserts the default value. But you haven't specified one, so the default is NULL.

How to insert varchar (date) to DateTime field in SQL table

I need to write an insert query to insert Date(string) to a DateTime field.
This is my table
CREATE TABLE [dbo].[TSTKSInterfaceRun](
ID INT IDENTITY(1,1),
AttendanceDate [datetime] NULL,
DateOfInterfaceRun [datetime] NULL,
TotalRecords [decimal](4,2) NULL,
Company [varchar](200) NULL
) ON [PRIMARY]
I have tried using this query
Insert into [TSTKSInterfaceRun] (AttendanceDate,DateOfInterfaceRun,TotalRecords,Company) VALUES(CONVERT(Datetime, '2017-05-01 18:01:00', 120),CONVERT(Datetime, '2017-05-01 23:00:00', 120),1500,'SANCO')
but not working.
Error show as
Arithmetic overflow error converting int to data type numeric.
in SSMS.
I need a query to insert a string(date) to DateTime column in SQL table
Thanks in advance
Your value 1500 is too large for a scale (4) and the precision(2). With (4,2) the max digits is 4 with 2 being decimal places. The maximum number that this field can hold is 99.99

SQL Server 2012 Column name or number of supplied values does not match table definition.

I'm still learning SQL so this is really basic. Im trying to get the Project Inserts to go into the project table, but Column name or number of supplied values does not match table definition. keeps on coming up can someone point out whats wrong
CREATE TABLE ASSIGNMENT (
ASN_NUM CHAR (2) NOT NULL CHECK (ASN_NUM IN ('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20')),
ASN_DATE VARCHAR (9) NOT NULL,
ASN_PRO_NUM CHAR (3) NOT NULL CHECK (ASN_PRO_NUM IN ('123','124','125','126','127','128')) REFERENCES PROJECT (PRO_NUM),
ASN_EMP_NUM CHAR (1) NOT NULL CHECK (ASN_EMP_NUM IN ('1','2','3','4','5','6','7')) REFERENCES EMPLOYEE (EMP_NUM),
ASN_JOB_CODE CHAR (3) NOT NULL CHECK (ASN_JOB_CODE IN ('ACT','DAN','DSD','DSS','NAD','NIN','SAN','SMO')) REFERENCES JOB (JOB_CODE),
ASN_HOURS DECIMAL (4,2) DEFAULT 0,
ASN_CHG_HR MONEY DEFAULT 0,
ASN_CHARGE MONEY DEFAULT 0,
PRIMARY KEY (ASN_NUM)
)
INSERT INTO ASSIGNMENT VALUES ('1','20-Jun-99','127','3','DSD','2.60',175.00,435.00);
INSERT INTO ASSIGNMENT VALUES ('2','20-Jun-99','123','6','SMO','1.80',85.00,53.00);
INSERT INTO ASSIGNMENT VALUES ('3','20-Jun-99','123','6','SAN','2.00',145.00,290.00);
INSERT INTO ASSIGNMENT VALUES ('4','20-Jun-99','126','2','NAD','3.70',120.00,444.00);
INSERT INTO ASSIGNMENT VALUES ('5','20-Jun-99','128','7','DSS','3.20',79.50,254.40);
INSERT INTO ASSIGNMENT VALUES ('6','20-Jun-99','127','2','DSD','2.90',175.00,507.50);
INSERT INTO ASSIGNMENT VALUES ('7','20-Jun-99','123','7','DSD','2.90',175.00,507.50);
INSERT INTO ASSIGNMENT VALUES ('8','20-Jun-99','125','5','NAD','1.50',120.00,180.00);
INSERT INTO ASSIGNMENT VALUES ('9','20-Jun-99','124','3','DSD','2.30',175.00,402.50);
INSERT INTO ASSIGNMENT VALUES ('10','20-Jun-99','127','5','NAD','4.30',120.00,516.00);
INSERT INTO ASSIGNMENT VALUES ('11','20-Jun-99','123','3','DSD','3.80',175.00,665.00);
INSERT INTO ASSIGNMENT VALUES ('12','21-Jun-99','126','7','DAN','1.50',95.00,142.50);
INSERT INTO ASSIGNMENT VALUES ('13','21-Jun-99','127','6','SAN','5.00',145.00,725.00);
INSERT INTO ASSIGNMENT VALUES ('14','21-Jun-99','125','3','DSD','2.90',175.00,507.50);
INSERT INTO ASSIGNMENT VALUES ('15','21-Jun-99','123','6','SMO','3.80',85.00,323.00);
INSERT INTO ASSIGNMENT VALUES ('16','21-Jun-99','128','5','NAD','4.10',120.00,492.00);
INSERT INTO ASSIGNMENT VALUES ('17','21-Jun-99','123','7','DAN','6.30',95.00,598.50);
INSERT INTO ASSIGNMENT VALUES ('18','21-Jun-99','125','2','NAD','11.20',120.00,1,344.00);
INSERT INTO ASSIGNMENT VALUES ('19','21-Jun-99','125','3','DSD','2.80',175.00,490.00);
INSERT INTO ASSIGNMENT VALUES ('20','01-Feb-99','123','1','SMO','2.00',85.00,170.00);
CREATE TABLE CLIENT (
CLI_NUM CHAR (3) NOT NULL CHECK (CLI_NUM IN ('112','114','115','118','122','123')),
CLI_LNAME VARCHAR (20) NOT NULL,
CLI_FNAME VARCHAR (20) NOT NULL,
CLI_MINI VARCHAR (10),
CLI_ADD VARCHAR (30) NOT NULL,
CLI_CITY VARCHAR (12) NOT NULL,
CLI_STATE CHAR (2) NOT NULL,
CLI_ZIP CHAR (5) NOT NULL,
CLI_AREA CHAR (3) NOT NULL,
CLI_PHONE CHAR (8) NOT NULL CHECK (CLI_PHONE LIKE ('[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]')),
PRIMARY KEY (CLI_NUM)
)
INSERT INTO CLIENT VALUES ('112','Chasteen','Anne','R','1234 Rose Lane','Murfreesboro','TN','37130','615','123-2345');
INSERT INTO CLIENT VALUES ('114','Williamson','Kirby','T','2345 Grove Street','Nashville','TN','32001','615','234-5678');
INSERT INTO CLIENT VALUES ('115','Quahtoty','hrman','','3456 Waterford Rd.','Nashville','TN','32008','615','239-9876');
INSERT INTO CLIENT VALUES ('118','Smith','Mary','M','4567 Oleander Blvd.','Owensboro','KY','38345','652','456-6543');
INSERT INTO CLIENT VALUES ('122','Taylor','Christopher','F','5678 Treeline Rd.','Murfreesboro','TN','37130','615','238-3344');
INSERT INTO CLIENT VALUES ('123','Trevors','Anne','K','6789 Sweetwater Dr.','Louisville','KY','39932','654','654-9001');
CREATE TABLE EMPLOYEE (
EMP_NUM CHAR (1) NOT NULL CHECK (EMP_NUM IN ('1','2','3','4','5','6','7')),
EMP_LNAME VARCHAR (20) NOT NULL,
EMP_FNAME VARCHAR (15) NOT NULL,
EMP_MINI VARCHAR (10),
EMP_DOB VARCHAR (35) NOT NULL,
EMP_VETERAN VARCHAR (3) NOT NULL CHECK (EMP_VETERAN IN ('YES','NO')),
PRIMARY KEY (EMP_NUM)
)
INSERT INTO EMPLOYEE VALUES ('1','Dempsey','John','R','Wednesday, December 02, 1970','No');
INSERT INTO EMPLOYEE VALUES ('2','Smithson','Susan','B','Saturday, April 11, 1964','Yes');
INSERT INTO EMPLOYEE VALUES ('3','Smith','James','D','Sunday, September 23, 1973','No');
INSERT INTO EMPLOYEE VALUES ('4','McDermott','Anne','W','Friday, November 28, 1975','No');
INSERT INTO EMPLOYEE VALUES ('5','Cheng','George','','Friday, July 09, 1971','Yes');
INSERT INTO EMPLOYEE VALUES ('6','Jackson','Marie','J','Saturday, October 16, 1971','No');
INSERT INTO EMPLOYEE VALUES ('7','Hernandez','Carlos','M','Friday, April 23, 1965','No');
CREATE TABLE JOB (
JOB_CODE CHAR (3) NOT NULL CHECK (JOB_CODE IN ('ACT','DAN','DSD','DSS','NAD','NIN','SAN','SMO')),
JOB_DESCRIPTION VARCHAR (45) NOT NULL,
JOB_CHG_HOUR MONEY DEFAULT 0,
PRIMARY KEY (JOB_CODE)
)
INSERT INTO JOB VALUES ('ACT','Accountant',75.00);
INSERT INTO JOB VALUES ('DAN','Data Analyst',95.00);
INSERT INTO JOB VALUES ('DSD','Database Systems Designer',175.00);
INSERT INTO JOB VALUES ('DSS','Decision Support Systems Specialist',79.50);
INSERT INTO JOB VALUES ('NAD','Network Administrator',120.00);
INSERT INTO JOB VALUES ('NIN','Network Installer',110.00);
INSERT INTO JOB VALUES ('SAN','Systems Analyst',145.00);
INSERT INTO JOB VALUES ('SMO','Statistical Modeler',85.00);
CREATE TABLE PROJECT (
PRO_NUM CHAR (3) NOT NULL CHECK (PRO_NUM IN ('123','124','125','126','127','128')),
PRO_NAME VARCHAR (20) NOT NULL,
PRO_CLI_NUM CHAR (3) NOT NULL CHECK (PRO_CLI_NUM IN ('118','122','114','112','118','114')) REFERENCES CLIENT (CLI_NUM),
PRO_COST MONEY DEFAULT 0,
PRO_OPEN_DATE VARCHAR (30) NOT NULL,
PRO_MANAGER CHAR (1) NOT NULL,
PRO_COORDINATOR CHAR (1) NOT NULL,
PRIMARY KEY (PRO_NUM)
)
INSERT INTO PROJECT VALUES ('123','Rock Veil','118',2,707.00,'Monday, May 12, 1997','2','5');
INSERT INTO PROJECT VALUES ('124','Willow Branch','122',785,000.00,'Monday, August 24, 1998','7','3');
INSERT INTO PROJECT VALUES ('125','Tin Roof','114',4,557,500.00,'Tuesday, May 12, 1998','6','5');
INSERT INTO PROJECT VALUES ('126','Freeze','112',1,080,000.00,'Tuesday, March 30, 1999','5','2');
INSERT INTO PROJECT VALUES ('127','Rumble Seat','118',3,010,000.00,'Thursday, June 11, 1998','2','6');
INSERT INTO PROJECT VALUES ('128','Fancy Flight','114',6,805,000.00,'Tuesday, June 22, 1999','5','7');
CREATE TABLE SKILL (
SKILL_EMP_NUM CHAR (1) NOT NULL CHECK (SKILL_EMP_NUM IN ('2','2','3','5','6','6','7','7','7')) REFERENCES EMPLOYEE (EMP_NUM),
SKILL_JOB_CODE CHAR (3) NOT NULL CHECK (SKILL_JOB_CODE IN ('DSD','NAD','DSD','NAD','SAN','SMO','DAN','DSD','DSS')) REFERENCES JOB (JOB_CODE),
SKILL_DATE VARCHAR (35) NOT NULL,
PRIMARY KEY (SKILL_EMP_NUM, SKILL_JOB_CODE)
)
INSERT INTO SKILL VALUES ('2','DSD','Monday, November 09, 1998');
INSERT INTO SKILL VALUES ('2','NAD','Friday, February 14, 1997');
INSERT INTO SKILL VALUES ('3','DSD','Thursday, April 11, 1996');
INSERT INTO SKILL VALUES ('5','NAD','Tuesday, December 05, 1995');
INSERT INTO SKILL VALUES ('6','SAN','Monday, July 23, 1990');
INSERT INTO SKILL VALUES ('6','SMO','Wednesday, September 02, 1992');
INSERT INTO SKILL VALUES ('7','DAN','Friday, November 28, 1997');
INSERT INTO SKILL VALUES ('7','DSD','Tuesday, June 22, 1999');
INSERT INTO SKILL VALUES ('7','DSS','Monday, May 17, 1993');
This is a little long for a comment.
When using insert, you should always be in the habit of including all the column names. So your first insert should look like:
INSERT INTO ASSIGNMENT(ASN_NUM, ASN_DATE, ASN_PRO_NUM, ASN_EMP_NUM, ASN_JOB_CODE, ASN_HOURS, ASN_CHG_HR, ASN_CHARGE)
VALUES ('1','20-Jun-99','127','3','DSD','2.60',175.00,435.00);
This won't necessarily fix the problem, but it will make it easier to debug.
To find the problem, run the code one table at a time to isolate the issue.