Anji007
Anji007

Reputation: 133

Oracle : Query to get the Last Friday of the week if I give any date

If I give a date let's say '13-Mar-2019' my query needs to retrieve the value '15-Mar-2019'. Which is the last Friday of the week.

Upvotes: 3

Views: 4008

Answers (4)

Gordon Linoff
Gordon Linoff

Reputation: 1269513

I would use next_day(). It is the Oracle function specifically designed for this purpose.

select next_day(date '2019-03-13', 'Fri')
from dual;

The only nuance is that if the date is Friday, then it will return the next Friday. That might be what you want. Otherwise, just subtract one day:

select next_day(date '2019-03-13' - 1, 'Fri') as friday_end_of_week
from dual;

Upvotes: 2

Ravshan Abdulaev
Ravshan Abdulaev

Reputation: 131

SELECT NEXT_DAY( to_date('2019-03-13', 'yyyy-mm-dd'), to_char(to_date('2019-03-01', 'yyyy-mm-dd'), 'DAY')) FROM dual;

demo

Upvotes: 0

Fahmi
Fahmi

Reputation: 37473

Try below -

select trunc(to_date('13-Mar-2019'), 'iw') + 4 from dual

Upvotes: 0

Thorsten Kettner
Thorsten Kettner

Reputation: 94859

Trancate the date to the ISO week, which gets you the week's Monday (as an ISO week starts with Monday). Then add four days:

select trunc(date '2019-03-13', 'iw') + 4 from dual;

Upvotes: 4

Related Questions