Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Substract Days to a Date in HIVE APACHE

How I can substract a number of days of a date, having as a result another date, for example: 01/12/2016 - 10 = 21/11/2016

like image 857
Diego Arias Avatar asked Oct 28 '25 05:10

Diego Arias


2 Answers

(date argument)

hive> select date_sub(date '2016-12-01',10);
OK
2016-11-21

or

(string argument)

hive> select date_sub('2016-12-01',10);
OK
2016-11-21

date_sub(date/timestamp/string startdate, tinyint/smallint/int days)

Subtracts a number of days to startdate: date_sub('2008-12-31', 1) = '2008-12-30'. Prior to Hive 2.1.0 (HIVE-13248) the return type was a String because no Date type existed when the method was created.

https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF

like image 92
David דודו Markovitz Avatar answered Oct 29 '25 18:10

David דודו Markovitz


there exist a hive udf to substract days to the hive datehttps://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF#LanguageManualUDF-DateFunctions, you have two options, transform your date to the following format to use the udf directly

yyyy-MM-dd

or you can transform your current date to timestamp and apply the udf, for example

date_sub(from_unixtime(unix_timestamp('12/03/2010' , 'dd/MM/yyyy')), 10) -- subs 10 days

I hope it helps, regards!

like image 38
hlagos Avatar answered Oct 29 '25 18:10

hlagos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!