Convert STRING to DATE (error 2666) - sql

in Teradata SQL I need convert a string to date.
Currently the string looks like this: 2017-02-28T14:41:32.817Z
But I need it in this format as DATE: DD.MM.YYYY HH:SS
Any idea how to do this? Whenever I try to cast, I get the error 2666 ( Invalid date supplied for mytable.mycolumn)
Hope someone can help!
Best regards,

Both input and expected result are timestamps, not dates.
SELECT '2017-02-28T14:41:32.817Z' AS mycol,
-- string to timestamp
Cast(mycol AS TIMESTAMP(3)),
-- back to string with a different format
To_Char(Cast(mycol AS TIMESTAMP(3)), 'DD.MM.YYYY HH:SS')

Related

MariaDB converting datetime string to date

Trying CONVERT()
Trying STR_TO_DATE()
Trying CAST() - neither DATE or DATETIME work
Trying CAST(STR_TO_DATE())
Hi, I cannot figure out what I'm doing wrong with trying to convert my varchar column Procedure_Date containing a string in the dd/mm/YYYY hh:mm:ss format. I am trying to extract just the date from this string but I seem to be cursed. Using MariaDB and have gone through all the docs there. Greatly appreciate any advice!
You can use cast
SELECT Procedure_Date, CAST(STR_TO_DATE(Procedure_Date, '%d-%m-%Y') AS DATE) FROM uniTable5;
Use: DATE('YOUR DATETIME')
EXAMPLE: DATE('2022-04-25 14:50:01')
OUTPUT: 2022-04-25. Result in DATE.

Getting null while converting string to date in spark sql

I have dates in the format '6/30/2020'. It is a string and I want to convert it into date format.
List of methods I have tried
Cast('6/30/2020' as date) #returns null
to_date('6/30/2020','yyyy/MM/dd') #returns null
I also tried splitting the string and then concatenating it into data.
After trying all this and putting all the possible combinations in the to_date function, I am still getting the answer as null.
Now I am confused as I have used all the functions to convert string to date.
Thanks in advance for your help.
The date format you used was incorrect. Try this:
select to_date('6/30/2020', 'M/dd/yyyy')
If you want to format your result, you can use date_format:
select date_format(to_date('6/30/2020', 'M/dd/yyyy'), 'yyyy/MM/dd')
Note that to_date converts a given string from the given format, while date_format converts a given date to the given format.

NULL values in a string to date conversion

I have a table with the following data:
logs.ip logs.fecha logs.metodo
66.249.93.79 19/Nov/2018:03:46:33 GET
All data columns are string and I want to convert logs.fecha into date with the following format: YYYY-MM-dd HH:mm:ss
I try the following query:
SELECT TO_DATE(from_unixtime(UNIX_TIMESTAMP(fecha, 'yyyy-MM-dd'))) FROM logs
Results of the query are NULL in all rows.
How can I make the conversion string to date for all rows? I know I must use ALTER TABLE but I don't know how to do it.
Thanks
The reason you get null is because the format of the input string is different from the input passed to unix_timestamp. The second argument to unix_timestamp should specify the string format of the first argument. In from_unixtime you can specify the output format desired. If nothing is specified, a valid input to from_unixtime returns an output in yyyy-MM-dd format.
The error can be fixed as below.
from_unixtime(unix_timestamp(fecha,'dd/MMM/yyyy:HH:mm:ss'),'yyyy-MM-dd HH:mm:ss')
You just have to tell Oracle the date format you are reading with TO_DATE.
Try:
SELECT TO_DATE(fecha,'DD/MON/YYYY:HH:MI:SS') FROM logs

convert string to date in hive

I have this date format in hive: 20180618151752
Make the following query to leave it in the following way:
select
concat(substr(xdr.time_stop,1,8),' ',
substr(xdr.time_stop,9,2),':',
substr(xdr.time_stop,10,2),':',
substr(xdr.time_stop,11,2)) as date
from padl.fraude_vozm_xdr;
20180618 18:17:52
Now, I need to convert that string field to date, how could I do it?.
Thank you
Use from_unixtime and unix_timestamp.
from_unixtime(unix_timestamp('20180618151752','yyyyMMddHHmmss'),'yyyyMMdd HH:mm:ss')

How to change date format in hive?

My table in hive has a filed of date in the format of '2016/06/01'. but i find that it is not in harmory with the format of '2016-06-01'.
They can not compare for instance.
Both of them are string .
So I want to know how to make them in harmory and can compare them. Or on the other hand, how to change the '2016/06/01' to '2016-06-01' so that them can compare.
Many thanks.
To convert date string from one format to another you have to use two date function of hive
unix_timestamp(string date, string pattern) convert time string
with given pattern to unix time stamp (in seconds), return 0 if
fail.
from_unixtime(bigint unixtime[, string format]) converts the
number of seconds from unix epoch (1970-01-01 00:00:00 UTC) to a
string representing the timestamp of that moment in the current
system time zone.
Using above two function you can achieve your desired result.
The sample input and output can be seen from below image:
The final query is
select from_unixtime(unix_timestamp('2016/06/01','yyyy/MM/dd'),'yyyy-MM-dd') from table1;
where table1 is the table name present in my hive database.
I hope this help you!!!
Let's say you have a column 'birth_day' in your table which is in your format,
you should use the following query to convert birth_day into the required format.
date_Format(birth_day, 'yyyy-MM-dd')
You can use it in a query in the following way
select * from yourtable
where
date_Format(birth_day, 'yyyy-MM-dd') = '2019-04-16';
Use :
unix_timestamp(DATE_COLUMN, string pattern)
The above command would help convert the date to unix timestamp format which you may format as you want using the Simple Date Function.
Date Function
cast(to_date(from_unixtime(unix_timestamp(yourdate , 'MM-dd-yyyy'))) as date)
here is my solution (for string to real Date type):
select to_date(replace('2000/01/01', '/', '-')) as dt ;
ps:to_date() returns Date type, this feature needs Hive 2.1+; before 2.1, it returns String.
ps2: hive to_date() function or date_format() function , or even cast() function, cannot regonise the 'yyyy/MM/dd' or 'yyyymmdd' format, which I think is so sad, and make me a little crazy.