how to get the last day of the quarter in hive

Viewed 152

I am trying to get the first and last date of a quarter using hive.

For first day of the quarter this works fine :

 select trunc(add_months(from_unixtime(unix_timestamp()),-(month(from_unixtime(unix_timestamp()))-1)%3),'MM')  as Firstday_quarter

But I am not able to retrive the last day of the quarter using hive. Ex: 2021-12-31 in this case

Could you please help me.

1 Answers

It is not so fancy, but I can get the first day of the quarter as;

hive> SELECT trunc(current_timestamp, 'Q');
OK
2021-10-01

And then based on first day I just got, by adding 75 days (just to make sure that I am on the last month of the quarter), I can get last day of the quarter/month;

hive> select last_day(date_add(trunc(current_timestamp, 'Q'), 75));
OK
2021-12-31
Related