How to convert this time to hive timestamp - hive

I have a timestamp value as below:
20171020T222028.026 GMT
I want to convert this to the following format:
2018-12-01 19:35:05
Is this possible in Hive ?
Input - 20171020T222028.026 GMT
Output - 2017-10-20 22:20:28

The input format needs to be parsed as shown here. No format parameter is needed for from_unixtime as the expected output is in the default output format yyyy-MM-dd HH:mm:ss
select from_unixtime(unix_timestamp('20171020T222028.026 GMT',"yyyyMMdd'T'HHmmss.SSS z"))
Formats:
'T' to escape the literal in the input value.
z for Timezone

Related

Conversion of datetime format

I have column name requestdatetime with data type string.
Value for requestdatetime is in format 15/Aug/2022:01:54:41 +0000
I need to convert 15/Aug/2022:01:54:41 +0000 into 'yyyy-MM-dd HH:mm:ss' format.
I have tried date_parse(requestdatetime,'%d/%b/%Y'':''HH:mm:ss'' ''+SSS') but it not working out.
You need to convert string to date then date to string to get expected result.
select date_format(parse_datetime('15/Aug/2022:01:54:41 +0000','dd/MMM/yyyy:HH:mm:ss Z'), '%Y/%m/%d %T')
result:
2022/08/15 01:54:41
date_parse accepts MySQL date format, try parse_datetime which accepts Java format (do not forget to add part for timezone offset - Z):
SELECT parse_datetime('15/Aug/2022:01:54:41 +0000', 'dd/MMM/yyyy:HH:mm:ss Z');
Output:
_col0
2022-08-15 01:54:41.000 UTC

Convert 'yyyymmddhhmmss' to MM/DD/YYYY HH:MM (AM/PM) in Snowflake

I would like to convert a String containing values in the format yyyymmddhhmmss to MM/DD/YYYY HH:MM (AM or PM) in Snowflake. The String contains time in UTC format. Also would like the final output to be in CST timezone.
Eg of String : 20220120035900
Expected output : 01/20/2022 03:59:00
Appreciate the help!
Please check below select try_to_timestamp('20220120035900', 'YYYYMMDDHHMISS');
UTC to CST:
SELECT try_to_timestamp('20220120035900', 'YYYYMMDDHHMISS') as t_str,
t_str::timestamp_ntz as UTC_tz,
convert_timezone('UTC','America/Chicago', UTC_tz) chicago_time;

In SQL How to convert time into UNIX timestamp

In hive there is some data I have. Now I want to convert the start_timestamp into unix_timestamp in second. How to do that? Because the start_timestamp has two formats:
First format:
2018-03-22 02:54:35
Second format:
May 15 2018 5:15PM
First format is 'yyyy-MM-dd HH:mm:ss', second is 'MMM dd yyyy hh:mm:aa'. If the format is wrong, unix_timestamp function will return NULL. Try to convert using one format, if NULL, try to convert using the other format. This can be done using coalesce function:
select
coalesce(unix_timestamp(start_timestamp ,'yyyy-MM-dd HH:mm:ss'),
unix_timestamp(start_timestamp ,'MMM dd yyyy hh:mm:aa')
) as UnixTimestamp
from my_table;
Use from_unixtime() to convert it back to given format if necessary, like in this answer.
See patterns examples here: SimpleDateFormat

How to convert date in YYYYMMDD in Hive to unix timestamp

I am trying to convert date in format YYYYMMDD in hive to unix_timestamp but when I do below, I am getting incorrect timestamp.
select unix_timestamp(DATE,'YYYYMMDD') from table_name.
For '20180301' I am getting unix timestamp output as '1514631600' which is DECEMBER 30,2017 11:59 pm
The format string should be yyyyMMdd.
select unix_timestamp(DATE,'yyyyMMdd') from table_name

Hive- Extract timestamp and date in defined format

I have column value in my HIVE table in String format like 20160921091213 i.e. YYYYMMDDHHMMDD. In target I have two columns one timestamp and other date column. I want to extract the same in the format for timestamp "YYYY-MM-DD HH24:MI:SS" and for date in the format "YYYY-MM-DD".
What can be the possible SQL for that.
convert to unix timestamp format and then convert back to string.
from_unixtime(unix_timestamp('20160921091213', 'yyyyMMddHHmmss'),'yyyy-MM-dd HH:mm:ss')
Result: 2016-09-21 21:12:13