syntax error when trying to input date and time - sql

I just started with SQL and I'm having a problem when trying to insert an date and time.
The table structure:
CREATE TABLE Voo_Pac
(
codReserva INT NOT NULL PRIMARY KEY,
DataCont DATE,
HoraCont TIME
);
Code I'm trying to use to insert date and time:
INSERT INTO Voo_Pac (codReserva, DataCont, HoraCont)
VALUES (1), (15-08-2019), (12:13:52);
When I try to execute the code, it gives me the following message:
Error 1: could not prepare statement (1 near ":13": syntax error)

I assume you are using MySQL/MariaDB/SQL Server because of the TIME datatype?
Your insert should be
INSERT INTO Voo_Pac (codReserva, DataCont, HoraCont)
VALUES (1, '2019-08-15', '12:13:52');
see demo

You at least need quotes. And depending on your DB maybe a CAST to the apropiated type
INSERT INTO Voo_Pac (codReserva, DataCont, HoraCont)
VALUES 1, '15-08-2019', '12:13:52';

Related

1064. You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'OUTPUT

i'm new to SQL and i'm getting this error for this query :
Insert into admin.MaintenanceWindows (StartTime, StopTime, Message, Status, AccessibilityLabel)
OUTPUT inserted.StartTime
values ('2022-12-17 03:47:00', '2022-12-18 03:47:00', 'Testdeclare', 'Scheduled', 'TEST');
i also tried
DECLARE #inserted table (StartTime int);
insert into admin.MaintenanceWindows (StartTime, StopTime, Message, Status, AccessibilityLabel)
output inserted.StartTime into #inserted
values ('2022-12-17 03:47:00', '2022-12-18 03:47:00', 'Testdeclare', 'Scheduled', 'TEST');
select * from #inserted;
i get error:- Declare is not valid along with Output is not valid
Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'OUTPUT inserted.StartTime values ('2022-12-17 03:47:00', '2022-12-18 03:47:00...' at line 2
Not sure what you tried to achive by the output term, but it looks like the right query should be without OUTPUT inserted.StartTime
Just-
-- create
CREATE TABLE MaintenanceWindows (
StartTime DATETIME NOT NULL,
StopTime DATETIME NOT NULL,
Message TEXT NOT NULL,
Status TEXT NOT NULL,
AccessibilityLabel TEXT NOT NULL
);
-- insert
INSERT INTO MaintenanceWindows values ('2022-12-17 03:47:00', '2022-12-18 03:47:00', 'Testdeclare', 'Scheduled', 'TEST') RETURNING StartTime ;
And the result from real compiler online (thanks to https://onecompiler.com/).
I changed your table from admin.MaintenanceWindows to MaintenanceWindows.
If you still have a compilation error, something is wrong with your table or with the data you are trying to insert into it.

"failed to evaluate expression" when fiddling with INSERT INTO on DataBricks SQL

I receive the following error when I try to insert into a casted date in my table
Error in SQL statement: AnalysisException: failed to evaluate expression to_date('01.01.2016', 'dd.mm.yyyy'): Cannot evaluate expression: to_date(01.01.2016, Some(dd.mm.yyyy)); line 2 pos 1
insert into E_Par_Holidays
values
('BE', to_date('01.01.2016', 'dd.mm.yyyy'));
The table is defined as:
create table E_Par_Holidays (
Country varchar(255),
Holiday date
);
And oddly the following SQL statement works like a charm:
SELECT to_date('01.01.2016', 'dd.mm.yyyy') as DateExample;
Thank you for your help!
I fixed it by using insert into select logic
insert into E_Par_Holidays
SELECT 'BE', to_date('01.01.2016', 'dd.mm.yyyy');
you shouldn't use insert statement in spark sql.
instead use dataframe writer api
dataframe.write.mode(SaveMode.append).jdbc(...)
You can use following: insert into alfiya.timestamptbl values(cast('0001-01-01 17:00:00.000 +0300' as timestamp ));

Bulk insert issue with date from excel into SQL Table

I am trying to bulk insert these two columns from excel into a temp table ##NBP_Table. However, when I do that I get the following error:
'Operand type clash: int is incompatible with date'
Does that mean the date aren't in the format it should be to be inserted into a table?
create table ##NBP_Table
(
Applicable_Date date,
NBP_Value numeric(4,4)
)
insert into ##NBP_Table
values (01/04/2014,1.7107),
(02/04/2014,1.6482),
(03/04/2014,1.686),
(04/04/2014,1.6681)
To get the date insert working, please try this
create table ##NBP_Table
(
Applicable_Date date
NBP_Value numeric(5,4)
)
insert into ##NBP_Table
values ('01/04/2014',1.7107)
The date needs to be in quotation marks
I have also corrected the numeric data type for you
this date in expression is considered as int so it will be performed / operations,
so please use 'before starting date and ' after ending date.
'01-04-2014'
Create table #NBP_Table
(
Applicable_Date date,
NBP_Value numeric(5,4)
)
insert into #NBP_Table
values ('01-04-2014',1.7107),
('02-04-2014',1.6482),
('03-04-2014',1.686),
('04-04-2014',1.6681)

Insert Operation Failed for SQL Server

I tried to insert some rows into SQL server database and throws an error:
Conversion failed when converting the varchar value 'null' to data type bit
Could anyone explain what is this for?
This is not in a program.
What you might be trying
INSERT INTO TESTTABLE(BITCOLUMN) VALUES('NULL')
What should it actually be..
INSERT INTO TESTTABLE(BITCOLUMN) VALUES(NULL)
If you want to insert a null value into a column, you need to write NULL, and not 'NULL' (wrapping it in quotes indicates that it is a VARCHAR containing the letters N, U, L L - not a null value).
That is what the error message is telling you: you're trying to insert the VARCHAR value 'NULL' into a column that will only accept bit data.
Change the 'NULL' in your insert query to NULL.
You need something like this:
INSERT INTO TableName(ColumnName) VALUES(NULL); --this is the right way
Instead of this:
INSERT INTO TableName(ColumnName) VALUES('NULL'); --this is the wrong way

SQL Server Compact. There was an error parsing the query

I cannot figure out why this is not working. I get the same thing when I try to do an update query as well.
Here is the error " There was an error parsing the query. [ Token line number = 1,Token line offset = 43,Token in error = where ] "
Here is the actual Query INSERT INTO ads (title,price,body,enabled,where,interval,posted) VALUES('test','899','test',True,'Columbus',15,'11/25/2009 10:12:30 AM')
Where would be 'Columbus'
I am using visual studio express 2008 C#
WHERE is a reserved word, try wrapping it in brackets
INSERT INTO ads (title,price,body,enabled,[where],interval,posted)
VALUES('test','899','test',True,'Columbus',15,'11/25/2009 10:12:30 AM')
i think you should provide the value of the primary key in your insert statement,maybe SQL Server Compact databases are not generated automatically or you dont configure that.
I had the same problem this is the INSERT statement which was not working and got the same error:
INSERT INTO Customers(CustomerName,CustomerAddress,CustomerPhone)
VALUES ('Osama','Amman','656565')
this is the INSERT statement which was working fine:
INSERT INTO Customers(CustomerID,CustomerName,CustomerAddress,CustomerPhone)
VALUES ('4564','Osama','Amman','656565')
also if you have in your table columns with names have spaces like (Customer Name)
you must use brackets in your sqlCe statement as:
INSERT INTO Customers([CustomerID],[Customer Name],[Customer Address],[Customer Phone])
VALUES ('4564','Osama','Amman','656565')
also if you use SELECT SCOPE_IDENTITY() to get last record inserted in INSERT Statement
as:
INSERT INTO Customers(CustomerID,CustomerName,CustomerAddress,CustomerPhone)
VALUES ('4564','Osama','Amman','656565') SELECT SCOPE_IDENTITY()
don't use it...