Improve your programming skills

HIVE – return closest Monday before date

How to return closest Monday date before, when date is given?

It is very simple, user next_day and date_add funtions

select next_day(date_add('YOUR_DATE',-7),'MONDAY')

Check

-- first next Monday od selected date
select next_day('2020-10-21', 'MONDAY');

-- returns closest Monday date before
select next_day(date_add('2020-11-15',-7),'MONDAY')
select next_day(date_add('2020-11-16',-7),'MONDAY')
select next_day(date_add('2020-11-17',-7),'MONDAY')
select next_day(date_add('2020-11-18',-7),'MONDAY')
select next_day(date_add('2020-11-19',-7),'MONDAY')
select next_day(date_add('2020-11-20',-7),'MONDAY')
select next_day(date_add('2020-11-21',-7),'MONDAY')
select next_day(date_add('2020-11-22',-7),'MONDAY')
select next_day(date_add('2020-11-23',-7),'MONDAY')

Leave a comment

Your email address will not be published.