简体   繁体   中英

Calculate Week Ending date in Oracle SQL where the week start is the previous SAT to the current FRI

I have a transaction date in my table that I would like to calculate a Week Ending date for

The problem is the weeks for us go from Saturday to Friday, don't ask me why,

So for example this week, today is 10/5/2020, the week end date should be 10/09/2020

and this Saturday, 10/10/2020, would be week ending date 10/16/2020.

Anyone know how to properly achieve this?

You could use next_day() :

select t.*,
    case when to_char(mydate, 'FMDAY') = 'FRIDAY' 
        then mydate
        else next_day(mydate, 'FRIDAY')
    end as endofweek
from mytable t

Note that both to_char(..., 'DAY') and next_day() are dependant on the language of your session. If your database or session are not in English language, you need to adujst the literal strings.

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