When I execute this statement in HIVE
select FROM_UNIXTIME(unix_timestamp(to_date('2016-03-28 00:00:00'),'YYYY-MM-DD'));
I get
OK
2015-12-27 00:00:00
Shouldn't it return 2016-03-28 00:00:00 instead?
The UPPER CASE string for the date pattern string that you have mentioned is wrong. It should be "yyyy-MM-dd"
Please use the following which should fix your error
select FROM_UNIXTIME(unix_timestamp(to_date('2016-03-28 00:00:00'),'yyyy-MM-dd'));
Related
im trying to get a date from a name i have got the date but i need it to be in yyyy-mm-dd format.
what i get:
20221201
what i need:
2022-12-01
please help me on this
Could be as simple as this ... assuming the date string is the last 8 characters
Example
Select try_convert(date,right('typographical_dismal_subjoinByapostrophize1_12345_20221201',8))
Resutls
2022-12-01
This should work ,
SELECT CONVERT(DATE,RIGHT('ABCDEF20221201',8),103)
I need to convert my varchar to date.
My varchar looks like this:
event_date
2020-09-20
2021-07-21
2021-01-20
2020-10-08
Here is my attempted solution:
SELECT
TO_DATE(event_date,'YYYY-MM-DD')
FROM
database.schema.table
Here is the error I get:
Can't parse ' ' as date with format 'yyyy-mm-dd'
Can anybody help me out? Thanks!
You can use try_to_date(event_date,'YYYY-MM-DD'), it will return NULL (will not fail) if event_date can not be parsed, you can filter and check what exactly it contains:
SELECT
event_date as varchar_date,
TRY_TO_DATE(event_date,'YYYY-MM-DD') as parsed_date
FROM
database.schema.table
WHERE TRY_TO_DATE(event_date,'YYYY-MM-DD') IS NULL --filter and check
After checking which formats do you have, you can parse different formats using coalesce:
coalesce(TRY_TO_DATE(event_date,'YYYY-MM-DD'),
TRY_TO_DATE(event_date,'YYYYMMDD')
)
Answer was the trim() function for the extra spaces around my varchar. Thanks everyone!
I was trying to convert a timestamp string with the following format into a timestamp format but encountered an error as such:
Invalid format: "20201216T090000+0000" is malformed at "0000+0000"
Original Query
SELECT from_iso8601_timestamp(date_ts)
FROM
...
I thought of applying substr to retrieve 20121216T09 only as a string, which I know would work with from_iso8601_timestamp. But any other advice would be appreciated!
The from_iso8601_timestamp() supports format with dashes and colons:
presto> SELECT from_iso8601_timestamp('2020-12-16T09:00:00+00:00');
_col0
-----------------------------
2020-12-16 09:00:00.000 UTC
In the case input is formatted like 20201216T090000+0000, the parse_datetime() would be more appropriate.
I want to modify the date eg:2018-02-02 00:00:00 to 20180202.What should I do.
I have used the to_date and regexp_replace function to modify the data, but it doesn't work.
What type of data is 2018-02-02 00:00:00 string or timestamp? maybe you should cast to string before use to_date.
I want to change string which is in format dd/mm/yyyy to date type in hive.
I am using hive version 1.0.0 . I have tried using:
TO_DATE(from_unixtime(UNIX_TIMESTAMP('07/03/2013', 'dd/mm/yyyy')))
But it returns NULL. Although it works with format 'dd-mm-yyyy'. But it returns NULL with 'dd/mm/yyyy'.
I have tried using CAST also but it also does not give me the correct result.
M - month
m - minutes
hive> select TO_DATE(from_unixtime(UNIX_TIMESTAMP('07/03/2013', 'dd/MM/yyyy'))) ;
OK
2013-03-07
or
hive> select TO_DATE(from_unixtime(UNIX_TIMESTAMP('07/03/2013', 'd/M/y'))) ;
OK
2013-03-07
or
hive> select cast (regexp_replace('07/03/2013','(..)/(..)/(....)','$3-$2-$1') as date);
OK
2013-03-07
I would try two options:
Check if simple replace works:
TO_DATE(from_unixtime(UNIX_TIMESTAMP(replace('07/03/2013', '/', '-'), 'dd-MM-yyyy')))
Take a look at date documentation to check if any function will work for you