Can't run SQL query due to type conversion failure, key violation, and more - sql

I get an error when trying to run this SQL query in MS Access:
INSERT INTO Learner (learnerPersonIDPKFK, registrationDate)
VALUES (1, 21/09/2015);
INSERT INTO Learner (learnerPersonIDPKFK, registrationDate, recommendedByLearnerPersonIDFK)
VALUES (2, 05/03/2016, 1);
This is the error:
enter image description here
The Learner table looks like this:
CREATE TABLE Learner
(
learnerPersonIDPKFK INT NOT NULL PRIMARY KEY,
registrationDate DATETIME,
recommendedByLearnerPersonIDFK INT NOT NULL,
CONSTRAINT fk_recommendedByLearnerPersonIDFK
FOREIGN KEY(recommendedByLearnerPersonIDFK)
REFERENCES Learner (learnerPersonIDPKFK)
);

Try wrapping your dates in single quotes:
INSERT INTO Learner (learnerPersonIDPKFK, registrationDate)
VALUES (1, '21/09/2015');
INSERT INTO Learner (learnerPersonIDPKFK, registrationDate, recommendedByLearnerPersonIDFK)
VALUES (2, '05/03/2016', 1);
Depending on your localization settings for the DATETIME format, you may need to use the MM/DD/YYYY format. Remember, your passing in a varchar and letting SQL try to "guess" how to implicitly convert it:
INSERT INTO Learner (learnerPersonIDPKFK, registrationDate)
VALUES (1, '09/21/2015');
INSERT INTO Learner (learnerPersonIDPKFK, registrationDate, recommendedByLearnerPersonIDFK)
VALUES (2, '03/05/2016', 1);
Use a simple tester to verify if SQL can "guess" your date formating:
SELECT CAST('09/21/2015' AS DATETIME)

Related

Select all from drivers table in database

I am writing a code to insert new drivers and vehicles into my database and along the way I came up with an error. Below is the code to create the drivers and vehicles table in my database.
create table drivers (
id serial primary key,
first_name varchar,
last_name varchar
);
create table vehicles (
id serial primary key,
make varchar,
model varchar,
driver_id integer references drivers(id)
);
Below is my INSERT statement
INSERT INTO drivers (first_name, last_name)
VALUES
('Amy', 'Hua'),
('UDOETE', 'AKAN'),
('UCHE', 'CALEB'),
('TERKIMBI', 'VANGE'),
('PETER', 'O. OKEOWO'),
('OTOGO', 'IRINEN'),
('OSAKA', 'GEORGE C.');
SELECT * from drivers;
INSERT INTO vehicles (make, model, driver(id))
VALUES
('2023 Acura', 'Integra', 1),
('2022 Acura', 'MDX', 2),
('2022 Acura', 'NSX', 3);
SELECT * from vehicles;
RETURNING *;
I was asked to Select all driver records; select all vehicle records; select only 3 vehicle records. But after writing the above code into it, it gave this error ERROR: syntax error at or near "(" Position: 44 and my code is just 22 lines.
Thanks in advance for your help.
'44' refers to the character position within the statement that caused the error:
yours:
INSERT INTO vehicles (make, model, driver(id))
must be:
INSERT INTO vehicles (make, model, driver_id)
This is my final code below, I removed the RETURNING statement and only displayed it with the SELECT statement.
INSERT INTO drivers (first_name, last_name)
VALUES
('Amy', 'Hua'),
('UDOETE', 'AKAN'),
('UCHE', 'CALEB'),
('TERKIMBI', 'VANGE'),
('PETER', 'O. OKEOWO'),
('OTOGO', 'IRINEN'),
('OSAKA', 'GEORGE C.');
SELECT * from drivers;
INSERT INTO vehicles (make, model, driver_id)
VALUES
('2023 Acura', 'Integra', 17),
('2022 Acura', 'MDX', 18),
('2022 Acura', 'NSX', 19);
SELECT * from vehicles;

having (Conversion failed ) error in SQL Server

When I create a table using bellow SQL query:
CREATE TABLE student(studentID INTEGER PRIMARY KEY,studnetName VARCHAR (30) NOT NULL,birthday Date)
Bellow operations(SQL commands) show me an error:
INSERT INTO student VALUES('226745','Ahmed','24-06-1997')
INSERT INTO student VALUES('226745','sara','28-03-2000')
INSERT INTO student VALUES('226745','Ali','12-02-2007')
the exact error is:
( Conversion failed when converting date and/or time from character string).
Can anybody help to explain why it happens?
It's because sql engine can't figure out the date format automatically (probably confused between m-d-y and d-m-y)
so you can specify the dateformat before you insert, ex in sql server:
set dateformat dmy;
and the same situation in other dbms
You don't specify your database. Most databases support strings in the format 'YYYY-MM-DD' (an ISO 8601-compatible format) for date constants:
INSERT INTO student VALUES('226745', 'Ahmed', '1997-06-24');
INSERT INTO student VALUES('226745', 'sara', '2000-03-28');
INSERT INTO student VALUES('226745', 'Ali', '2007-02-12');
In Standard SQL, date constants are supported by using the date keyword:
INSERT INTO student VALUES('226745', 'Ahmed', DATE '1997-06-24');
INSERT INTO student VALUES('226745', 'sara', DATE '2000-03-28');
INSERT INTO student VALUES('226745', 'Ali', DATE '2007-02-12');
I would also advise you to list the columns explicitly when using insert.

EXPLAIN Failed. 3706: (-3706)Syntax error: expected something between ')' and ','

I've seen this error mentioned on StackOverflow, but not in the context I am using. I am relatively new to Teradata and this behavior is throwing me for a loop. Here is code that works:
INSERT INTO test_table (this, that) VALUES (1, 2);
Here is code that throws the error:
INSERT INTO test_table (this, that) VALUES (1, 2), (3, 4);
This is super confusing to me because the Teradata docs have the following example:
INSERT INTO cities VALUES (2, 'San Jose'), (3, 'Oakland');
Could someone show me what am I missing here? Thanks!
Teradata only allows you to insert one record with a single values. You can see this in the syntax diagram for insert -- there is no "backloop".
Two inserts is a simple workaround:
INSERT INTO test_table (this, that)
VALUES (1, 2);
INSERT INTO test_table (this, that)
VALUES (3, 4);

What's wrong with this SQL INSERT statement?

I want to insert a certain value into a certain field of five different rows altogether. But whenever I run this query it doesn't get executed. What's wrong with it, and how can I fix it?
INSERT INTO `employee`(`password`) VALUES ('abc') WHERE `id` IN (1,2,3,4,5);
An INSERT can't have a WHERE clause. It looks like you meant to do an update:
UPDATE `employee`
SET `password` = 'abc'
WHERE `id` IN (1,2,3,4,5);
Or perhaps a multi-row insert:
INSERT INTO `employee` (`id`, `password`)
VALUES (1, 'abc'), (2, 'abc'), (3, 'abc'), (4, 'abc'), (5, 'abc');
Also, FYI, you really shouldn't be storing passwords as plain text, which it looks like you may be doing.

Informix Blob data

I need to know if there is any possibility of passing null in blob data using insert statement in Informix.
I know there is an option of LOAD FROM <FILE> INSERT INTO <TABLE>
Which works well, but i am looking for some insert statment, because i am implementing liquibase and i am not finding any way to insert null or leave it in insert values so that it can accept null. This field is set to TRUE for null values.
Error I am getting is following
[Error Code: -617, SQL State: IX000] A blob data type must be supplied within this context.
Tried this
INSERT INTO informix.tti_key_store (id, code, description, value_asc, cts, active)
VALUES
(4, 'EVDSDEALER', 'EVDS Dealer Passphrase', 'nodYzUNAs+htD1Mng3hYYg==', DATETIME (2016-02-17 12:51:34.000) YEAR TO FRACTION(5), 'T')
Tried this
INSERT INTO informix.tti_key_store (id, code, description, value_asc, value_bin, cts, active)
VALUES
(4, 'EVDSDEALER', 'EVDS Dealer Passphrase', 'nodYzUNAs+htD1Mng3hYYg==', NULL, DATETIME (2016-02-17 12:51:34.000) YEAR TO FRACTION(5), 'T')
value_bin is a blob field set to NULL to true.