extract the date from a timestamp value variable in Impala - sql

How can I extract the date from a timestamp value variable in Impala?
eg time = 2018-04-11 16:05:19 should be 2018-04-11

try this:
from_timestamp('2018-04-11 16:05:19','yyyy-MM-dd') as date_value
from_timestamp(datetime timestamp, pattern string): Converts a TIMESTAMP value into a string representing the same value. Please see documentation here

to_date (t1.local_time), as date_value
to_date: Returns a string representation of the date field from a timestamp value.

Related

What is the alternative of TRUNC(DATE) in Hive?

I have Oracle SQL query where it has been used TRUNC(04-Aug-2017 15:35:32)
What will be parameter in Hive to replace TRUNC?
Assuming you have a date/time, you can use the to_date() function:
select to_date(col)
If you have a timestamp, say ts, you can use trunc():
trunc(ts, 'day')
This returns a timestamp, with the time portion stripped off - which is similar to what trunc() does in Oracle when given one argument only.
On the other hand, you can also convert the timestamp to a date:
to_date(ts)
This returns a date rather than a timestamp: that's a different datatype, that has no time component (Oracle does not have such a datatype: both date and timestamp store the date and time).
As per Oracle docs, The TRUNC (date) function returns date with the time portion of the day truncated to the unit specified by the format model fmt. The value returned is always of datatype DATE, even if you specify a different datetime datatype for date. If you omit fmt, then date is truncated to the nearest day.
Similar is the function of to_date function in Hive.
It returns the date part of a timestamp string (pre-Hive 2.1.0): to_date("1970-01-01 00:00:00") = "1970-01-01".
If what you want is the timestamp(midnight timestamp : 00:00:00) along with the truncated date, you need to use some conversions as shown below:
cast(from_unixtime(unix_timestamp(to_date(<YOU_DATE_COL>), 'yyyy-MM-dd')) as timestamp)

Convert Unixtime to MMddyyyy

I'm trying to convert a column which has unixtime (ex 1542862806000) to regular DTS
select unix_timestamp(column_name) from table;
But i get error:
AnalysisException: No matching function with signature: unix_timestamp(BIGINT).
My column type is bigint
You are looking for from_unixtime not unix_timestamp.
select from_unixtime(cast(column_name/1000 as bigint),'MMddyyyy')
from table
unix_timestamp converts a date/date format string to a bigint representing the number of seconds since 1970-01-01 00:00:00 UTC.
from_unixtime takes a bigint input and converts it to the required date format.

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.

Retrieving the month from the timestamp format "25/Nov/2016:15:48:01 +0000'

I'm currently working with Hive SQL and I have the 'timestamp' column in a table with the format "25/Nov/2016:15:48:01 +0000".
How can I extract the month which is 'Nov' from the above format without using SUBSTRING() function.
I tried,
SELECT MONTH(timestamp) FROM table_name;
But it returned as null. What should be the correct way to retrieve the month which is 'Nov' from the above timestamp format.
Please see the below example..
unix_timestamp(time,'dd/MMM/yyyy:HH:mm:ss') and then try your month function
OR
First convert your timestamp to string to apply month function below like this
example
string 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 in the format of "1970-01-01 00:00:00"
then you can apply month function which will return month int format which can be converted equivalent String later
int month(string date) Returns the month part of a date or a
timestamp string: month("1970-11-01 00:00:00") = 11,
month("1970-11-01") = 11.

Hive from_unixtime for milliseconds

We have a timestamp epoch column (BIGINT) stored in Hive.
We want to get Date 'yyyy-MM-dd' for this epoch.
Problem is my epoch is in milliseconds e.g. 1409535303522.
So select timestamp, from_unixtime(timestamp,'yyyy-MM-dd') gives wrong results for date as it expects epoch in seconds.
So i tried dividing it by 1000. But then it gets converted to Double and we can not apply function to it. Even CAST is not working when I try to Convert this double to Bigint.
Solved it by following query:
select timestamp, from_unixtime(CAST(timestamp/1000 as BIGINT), 'yyyy-MM-dd') from Hadoop_V1_Main_text_archieved limit 10;
The type should be double to ensure precision is not lost:
select from_unixtime(cast(1601256179170 as double)/1000.0, "yyyy-MM-dd hh:mm:ss.SSS") as event_timestamp
timestamp_ms is unixtime in milliseconds
SELECT from_unixtime(floor(CAST(timestamp_ms AS BIGINT)/1000), 'yyyy-MM-dd HH:mm:ss.SSS') as created_timestamp FROM table_name;
In the original answer you'll get string, but if you'd like to get date you need to call extra cast with date:
select
timestamp,
cast(from_unixtime(CAST(timestamp/1000 as BIGINT), 'yyyy-MM-dd') as date) as date_col
from Hadoop_V1_Main_text_archieved
limit 10;
Docs for casting dates and timestamps. For converting string to date:
cast(string as date)
If the string is in the form 'YYYY-MM-DD', then a date value corresponding to that year/month/day is returned. If the string value does not match this formate, then NULL is returned.
Date type is available only from Hive > 0.12.0 as mentioned here:
DATE (Note: Only available starting with Hive 0.12.0)