Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference in days between two dates in Impala

Tags:

date

impala

I am trying to find a date difference In Impala. I have tried a few options. my most recent is below

ABS(dayofyear(CAST(firstdate AS TIMESTAMP)-dayofyear(CAST(seconddate AS TIMESTAMP)

an example of data looks like:

firstDate: 2017-11-25 secondDate: 2017-11-30

The solution that I am looking for in the above scenario would be five. The provided code would give me this answer, but the trouble comes in when the first date is in december and the second one ends in January.

Firstdate: 2016-12-30 seconddate:2017-12-30

with the above code this gives me an output of 358 days,the outcome that I would want to see is 8 days. This code is only looking at what day of the year this falls on, and is not able to consider moving from year to year. I know that there is no datediff function on Impala and I've also tried using just subtraction on the two timestamps but that didn't work either. I have scoured google and have come back empty any suggestions would be greatly appreciated

like image 812
burnsa9 Avatar asked Dec 04 '17 19:12

burnsa9


People also ask

How do you tell the time difference on an Impala?

unix_timestamp(finish_time)-unix_timestamp(start_time) will give you the seconds between them.

Does Impala support date data type?

The DATE type is available in Impala 3.3 and higher.

How do you trunc a date on an Impala?

You should use trunc(timestamp, 'J') . See the documentation for Day, you're using 'Starting day of the week': trunc(timestamp, string unit) Purpose: Strips off fields from a TIMESTAMP value. Unit argument: The unit argument value is case-sensitive.


1 Answers

SQL

SELECT DATEDIFF(TO_DATE(firstdate), TO_DATE(seconddate));

Is this what you are looking for?

like image 183
Ramesh Avatar answered Oct 12 '22 12:10

Ramesh