Convert function SQL Server - sql

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

Related

"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 ));

How to store temp generated output in SQL into Table

i have a sql statement, which the result of caclulations i want to write into a table.
i got an result for temp. But unluckily i cant transfer these results into a new table, because its saying that temp is not defined.
SELECT BUM.LMISTAT_import.UhrzeitBeginn, BUM.LMISTAT_import.UhrzeitEnde,BUM.LMISTAT_import.DatumEnde,BUM.LMISTAT_import.DatumBeginn,
TIMESTAMPDIFF(DAY, CAST(CONCAT(RIGHT(BUM.LMISTAT_import.DatumBeginn,2),'-',MID(BUM.LMISTAT_import.DatumBeginn,4,2),'-',LEFT(BUM.LMISTAT_import.DatumBeginn,2)) AS DATETIME),
CAST(CONCAT(RIGHT(BUM.LMISTAT_import.DatumEnde,2),'-',MID(BUM.LMISTAT_import.DatumEnde,4,2),'-',LEFT(BUM.LMISTAT_import.DatumEnde,2)) AS DATETIME)) AS temp
FROM BUM.LMISTAT_import;
INSERT INTO BUM.LMISTAT_import(BUM.LMISTAT_import.factor)
VALUES (temp)
;
actural SQL Result = "1054: Unknow column temp in field list";
the expected result is, that the table ..._import.factor is filled with values from temp.
Are you looking for create table as?
CREATE TABLE temp AS
SELECT i.UhrzeitBeginn, i.UhrzeitEnde,
i.DatumEnde, i.DatumBeginn,
TIMESTAMPDIFF(DAY,
CAST(CONCAT(RIGHT(i.DatumBeginn, 2), '-', MID(i.DatumBeginn,4,2), '-', LEFT(i.DatumBeginn,2)) AS DATETIME),
CAST(CONCAT(RIGHT(i.DatumEnde, 2), k'-', MID(i.DatumEnde, 4, 2), '-', LEFT(i.DatumEnde,2)) AS DATETIME)
) AS temp
FROM BUM.LMISTAT_import i;
You probably want SELECT...INTO.
In your first query, "AS temp" creates an alias for your result set, not a column list, which "VALUES (...)" requires, but which won't work anyway here.
Consider the syntax
INSERT INTO temp (a,b,c)
SELECT x,y,z ...;
Or even
CREATE TABLE temp (a,b,c)
SELECT x,y,z ...;
Try to go back into DatumBeginn and store it as a DATE that is yyyy-mm-dd, not some other form.
Failing that, use STR_TO_DATE() as a much more concise way to parse malformed date strings.
And a properly formed string does not need CAST( ... AS DATE)

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

Using CURRENT_TIMESTAMP result to insert in an INSERT INTO statement

I was trying to execute the following query:
DECLARE #MyValue DATETIME
SET #MyValue = CAST((SELECT CURRENT_TIMESTAMP) as DATETIME)
INSERT INTO Person (Gender,Name, JoinDate)
VALUES ('M','Alfred',#MyValue)
but SQL Server keeps throwing the following error:
Msg 241, Level 16, State 1, Procedure Insert_Current_Time, Line 8
Conversion failed when converting date and/or time from character
string.
I've already tried many other things, but none of them seemed to solve it. Is there any solution for this?
Just do this:
INSERT INTO Person (Gender,Name, JoinDate)
VALUES ('M','Alfred',getdate())
or this:
INSERT INTO Person (Gender,Name, JoinDate)
VALUES ('M','Alfred',current_timestamp)
Can you put the CURRENT_TIMESTAMP into your insert and dispense with the cast?
This works for me:
create table flar ( thetime datetime);
insert into flar values( CURRENT_TIMESTAMP);
Is JoinDate a string/varchar datatype? If so you need to cast it to a string instead of trying to store a datetime value in a string field.
Check this out: How to convert DateTime to VarChar

ServiceStack OrmLite and DateTimeOffset support in the UK

I am getting an issue with OrmLite and DateTimeOffset support. I am based in the UK and believe that it is related.
I have a table with a column of type DateTimeOffset.
I get the following SQL error when trying to insert into the DateTimeOffset column:
Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.
I have run SQL Profiler and can see that the SQL being executed is as follows:
INSERT INTO "Table"
("InsertedDateTime")
VALUES
('23/04/2013 09:30:48 +00:00')
I am pretty sure that this is an issue with the dd/mm/yy vs mm/dd/yy. If I convert the SQL to the following, it works fine:
INSERT INTO "Table"
("InsertedDateTime")
VALUES
('23-Apr-2013 09:30:48 +00:00')
Have I got something configured incorrectly or is there something I need to do to get this to work correctly?
You just need to change the default date format. Try this one -
SET DATEFORMAT dmy
DECLARE #temp TABLE (col DATETIMEOFFSET)
INSERT INTO #temp (col)
SELECT '23-Apr-2013 09:30:48 +00:00'
INSERT INTO #temp (col)
SELECT '2013-04-23 09:30:48 +00:00'
INSERT INTO #temp (col)
SELECT '2013/04/23 09:30:48 +00:00'
INSERT INTO #temp (col)
SELECT '23/04/2013 09:30:48 +00:00'