Possible Duplicate: How to get previous day date in Hive
To answer your question, date_sub will fetch you the previous day record. It works only on your date value in format: yyyy-MM-dd.
Syntax of date_sub is as:
date_sub(String date, Int days)
Hence you need to first convert your current date format into yyyy-MM-dd
format. To achieve this, use the below query:
SELECT from_unixtime(unix_timestamp('20180821','yyyyMMdd'),'yyyy-MM-dd' as converted_date_format;
Next you apply date_sub to the above result set with a 1 day value to get the previous record.
select date_sub(from_unixtime(unix_timestamp('20180821','yyyyMMdd'),'yyyy-MM-dd'),1) as previous_day;
Having said that, if your requirement is to maintain the date format as yyyyMMdd
, you can apply regex_replace function to remove the '-', as below:
select regexp_replace(date_sub(from_unixtime(unix_timestamp('20180821','yyyyMMdd'),'yyyy-MM-dd'),1),'-','') as previous_day_formatted_yyyymmdd;
Hope this helps :)