How to convert date in YYYYMMDD in Hive to unix timestamp - hive

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

Related

Convert date to YYYY-MM-DDThh:mm:ss.sssZ format in Postgresql

I want to convert the date format to YYYY-MM-DDThh:mm:ss.SSSXXXZ (ISO 8601).
Getting 2021-02-10T04:02:55.55SXXXZ and it's wrong. I need it as 2021-02-10T04:02:55.55S000000Z.
select to_char(now(), 'YYYY-MM-DDThh:mm:ss.SSSXXXZ')
For ISO 8601 date format you can use below query:
select to_char(now(), 'YYYY-MM-DD"T"HH24:MI:SS"Z"')
Output:
"2021-02-10T12:09:17Z"
Is it your desired format?

Convert UTC timestamp to yyyyMMddHHmmss.SSS format in hive

I have a scenario like below in hive
convert the current_timestamp to UTC. I am able to do so
select to_utc_timestamp(current_timestamp, 'America/Los_Angeles)';
Result:
2020-02-04 10:00:06.162
Next convert this resulting timestamp to yyyyMMddHHmmssSSS format.
I have tried like below
select from_unixtime((to_utc_timestamp(current_timestamp, 'America/Los_Angeles)', 'yyyy-MM-dd HH:mm:ss.SSS'), 'yyyyMMddHHmmssSSS');
I am unable to get the desired result.
expected result is 20200204100006162
You can use the date_format function if the Hive version >= 1.2.0.
select date_format(to_utc_timestamp(current_timestamp, 'America/Los_Angeles'),'yyyyMMddHHmmssSSS')

How to convert the date July 1, 2017 to dd-MM-yyyy using Hive SQL?

I have a Hive table with a Week column having values such as:
I have to convert this field to a date format such as: 2017-07-01 (yyyy-MM-dd) using hive SQL.
Any suggestions?
You can use a combination of from_unixtime and unix_timestamp.
select from_unixtime(unix_timestamp(weekCol,'MMM dd, yyyy'),'yyyy-MM-dd')
Use a combination of unix_timestamp and from_unixtime
select from_unixtime(unix_timestamp(week,'MMMM dd, yyyy'),'yyyy-MM-dd') from table_name;
unix_timestamp(string datetime, string pattern) converts datetime with given pattern to unix time stamp.
from_unixtime(bigint unixtime[, string format]) converts the number of seconds from unix epoch.

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

How can I convert gregorian date to julian date in Hive

I have a table with column name "date". The date is structured as YYYY-MM-DD and i need to convert it to YYYYDDD
I don't think hive has any simple quick way of doing this..
Using hive version 0.13.0
You can do this with the unix timestamp functions. First defining your date format and converting to a unix epoch timestamp, and then converting the unix timestamp into the Julian date format.
-- this would give the output of 2016096
select from_unixtime(unix_timestamp('2016-04-05','yyyy-MM-dd'), 'yyyyDDD') from yourTableName