How to subtract months from date in HIVE

五迷三道 提交于 2019-12-04 15:36:16
Manoj R
 select add_months('2015-02-01',-2);

if you need to go back to first day of the resulting month:

 select add_months(trunc('2015-02-01','MM'),-2);

Please try add_months date function and pass -2 as months. Internally add_months uses Java Calendar.add method, which supports adding or subtracting (by passing negative integer).

https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF#LanguageManualUDF-DateFunctions

--in Hive if the date format is in YYYY-MM-DD format, it becomes really easy to use difference in terms on month and year

from_unixtime(unix_timestamp(2015-02-01,'yyyy-mm-dd')-2*30*24*60*60, 'yyyy-MM-dd');

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!