"failed to evaluate expression" when fiddling with INSERT INTO on DataBricks SQL - 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 ));

Related

How to insert table in Databricks using magic SQL operator

I have create the following SQL table in databricks (using the magic %sql) as follows:
%sql
CREATE TABLE mytable (
id INT
,name STRING
,met_area_name STRING
,state STRING
,type STRING
) USING CSV
I am now trying insert data into the table using the following command:
%sql
INSERT INTO TABLE mytable VALUES (id,name,type)
SELECT DISTINCT criteria1, criteria2, 'b'
FROM tablex
WHERE somecriteria1 = 0
ORDER BY somecriteria2;
However, I'm getting the following error:
Error in SQL statement: ParseException:
mismatched input 'FROM' expecting <EOF>(line 2, pos 2)
== SQL ==
INSERT INTO TABLE mytable VALUES (id,name,type)
FROM tablex
--^^^
WHERE somecriteria1 = 0
ORDER BY somecriteria2
I'm sure there is something very obvious that I'm missing, but I can't see it.
Any assistance much appreciated.
Cheers

Error while insert into hive table using rand() function

I tried below snippet to insert random values in fileid column.
I got error like
//cannot recognize input near 'AS' 'floor' '(' in selection target)//
Can anyone help me out .
Select floor(RAND()*(99999-10000)+10000); //works fine though.
I only got issue at insert time.
INSERT INTO table test.a1
SELECT
Fileid AS floor(RAND()*(99999-10000)+10000)
FROM
test.a2;
You mixed up alias with column ref.
It should be:
INSERT INTO table test.a1
SELECT
Floor(RAND()*(99999-10000)+10000) as fileid
FROM
test.a2;

syntax error when trying to input date and time

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

Convert function SQL Server

I'm trying to convert the data of a column from varchar(255) to Timestamp.
I went to Microsoft documentation and it's not working
Code:
SELECT
["Data Nascimento"]
FROM
[leoninos] AS Original,
CONVERT(VARCHAR, ["Data Nascimento"]) AS VARCHAR,
CONVERT(timestamp(6, 4), ["Data Nascimento"]) AS timestamp;
Error:
Msg 156, Level 15, State 1, Line 53.
Incorrect syntax next to keyword 'CONVERT'.
What is the syntax error?
Thanks in advance.
Don't use varchar and timestamp as column aliases. Try this:
SELECT ["Data Nascimento"] as original
TRY_CONVERT(varchar(255), ["Data Nascimento"]) AS type_varchar
-- TRY_CONVERT(timestamp(6, 4), ["Data Nascimento"]) AS type_timestamp
FROM [leoninos] ;
Notes:
timestamp isn't really appropriate. I don't know what you are trying to do.
The FROM clause goes after the SELECT list.
Don't use SQL keywords as column names (even if they are not reserved).
Use try_convert() in case the conversion fails.
It's hard to tell what you are doing- but it appears that you are trying to select a table and also two scalars at once. Try just doing the CONVERT statements on their own with sample values:
SELECT CONVERT(int, '10'), CONVERT(date, '20180720')
Then, once you have it working for some sample values, you can convert column values:
CREATE TABLE #test (a NVARCHAR(max))
INSERT INTO #test VALUES ('january 1 2018')
INSERT INTO #test VALUES ('20180720')
SELECT a, CONVERT(date, a) FROM #test
DROP TABLE #test

In SQL,Why is the format 'YYYY-MM-DD' for DATE data type in an Insert statement giving error

When I tried this,
Insert into table abc values('name',2000,'1994-04-01');
I am getting an error showing that
"Literal does not match format string".
The third parameter is the date datatype in the insert statement. I tried googling it,but it's the same syntax everywhere.Please help!!
Try this one -
--INSERT INTO table_abc
SELECT 'name', 2000, to_date('1994-04-01', 'yyyy/mm/dd')
FROM DUAL D