简体   繁体   中英

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.

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;

尝试以下-

select trunc(to_date('13-Mar-2019'), 'iw') + 4 from dual
SELECT NEXT_DAY( to_date('2019-03-13', 'yyyy-mm-dd'), to_char(to_date('2019-03-01', 'yyyy-mm-dd'), 'DAY')) FROM dual;

演示

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM