Hive: Calculate exactly 1 year from date in format 'yyyy-MM-dd' string

Viewed 139

I need to calculate if has passed exactly 1 year or more from this date '2021-01-29', in HIVE. So the result date must be in 'yyyy-MM-dd' format, and equal to '2022-01-29' or later. '2022-01-28' it's not correct answer.

It's possible to use date_add('2021-01-29', interval 1 year), if so, could someone explain how?

Thank you in advance.

1 Answers

In newer versions of Hive since 1.2.0 you can add interval to the date:

select date('2021-01-29') + interval 1 year

Result:

2022-01-29

For old version of hive use this recipe:

1 Year = 12 months. Add 12 months using add_months function:

select add_months('2021-01-29',12)

Result:

2022-01-29

If you want to add more than one year, multiply 12 by the number of years.

Related