Skip to content
Advertisement

How to find previous date in HIVE

I want to find previous date in Hive.

For example :

I have Date as 20180821 (yyyyMMdd) format, I want to find previous date of this in Hive. I tried date_sub() function, seems it works only with yyyy-MM-dd format.

How to solve my issue. Please help

Advertisement

Answer

In addition to already provided solution with unix_timestamp conversion, please consider also this. The same conversion using substr() looks a little bit longer, but it will work with any dates/timestamps like '19691231 19:00:00' without TZ issues, also this is not slower, because not using SimpleDateFormat/etc and not applying TZ.

date_sub(concat_ws('-', substr('20180821',1,4), substr('20180821',5,2), substr('20180821',7,2)),1)
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement