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

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;

Related

Impala: convert dd-mm-yy hh:mm:ss string to Date yyyy-mm-dd HH:mm:ss.SSS format

I have a impala table where date column values are stored in dd-mm-yy HH:mm:ss string format, e.g.
30-11-20 12:34:45
I want to convert them into yyyy-mm-dd HH:mm:ss.SSS, e.g.
2020-11-30 12:34:45.000
If anyone could suggest a way to achieve this!!
first use to_timestamp to convert to timestamp. Then use from_timestamp to convert to string.
select from_timestamp(
to_timestamp('30-11-20 12:34:45','dd-MM-yy HH:mm:ss')
,'yyyy-MM-dd HH:mm:ss.SSS') as str_timestamp

How to convert this time to hive timestamp

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

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

TIMESTAMP in hive?

I have one column , data as 'Apr 06 2016 05:30:30' it is not in the time stamp formate, when using this one as timestamp I am getting null values. So stored as string, now I want to do some calculation on this when it is in time stamp formate. for that i converted into unixtimestamp and getting back to timestamp formate but the value of the date is changed. I used conversions as 'select from_unixtime(unix_timestamp(start_time, 'MMM DD YYYY HH:mm:ss')) from temp;'
I got value as '2015-12-27 05:30:30'.
I want final data as '2016-04-06 05:30:30'.
Please help me on this
You have just written the wrong format. The proper format string is 'MMM dd yyyy HH:mm:ss'. Take a look at https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html for reference for format strings.