Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get date difference in minutes using Hive

Below query is my sql server query and I want it to convert it into hive query:

select DATEDIFF([minute], '19000101', '2013-01-01 10:10:10')
like image 655
displayname Avatar asked Nov 08 '15 10:11

displayname


People also ask

How do you find the date difference in Hive?

Solution. datediff function in Hive takes 2 dates in String type and gives you the difference between the dates.

How do I calculate the difference between two Hive timestamps?

If you need the difference in seconds (i.e.: you're comparing dates with timestamps, and not whole days), you can simply convert two date or timestamp strings in the format 'YYYY-MM-DD HH:MM:SS' (or specify your string date format explicitly) using unix_timestamp(), and then subtract them from each other to get the ...

How do I get a timestamp on my Hive?

Solution. CURRENT_DATE will give the current date and CURRENT_TIMESTAMP will give you the date and time. If you want to work with EPOCH time then use unix_timestamp() to get the EPOCH time and use from_unixtime to convert EPOCH to date and time.

How do you subtract in Hive?

Apache Hive does not support MINUS set operator. If you have any requirement to perform MINUS, then you have to rewrite your queries using an alternate method. There are two methods that you can use: Use LEFT OUTER JOIN.


1 Answers

You could use unix_timestamp for dates after 1970:

SELECT (unix_timestamp('2013-01-01 10:10:10') 
      - unix_timestamp('1970-01-01 00:00:00'))/60 
  1. Convert both dates to seconds from 1970-01-01
  2. Substract them
  3. Divide by 60 to get minutes

EDIT:

Adding Minutes: change date to unixtime -> add var * 60sec -> convert back to date

SELECT from_unixtime(unix_timestamp('2013-01-01 10:10:10') + 10 * 60) AS result

db<>fiddle demo using MySQL

like image 56
Lukasz Szozda Avatar answered Oct 12 '22 16:10

Lukasz Szozda