Raúl Castro Raúl Castro - 4 months ago 7
SQL Question

How to construct a date knowing the Day of the week in Oracle

I am trying to construct a date in oracle.

I know that the Day of the Week is 2 (Monday in my case), the year and month can be the current ones.
So for example if today is July 24, and I need to construct a date for Monday (2) so it becomes '18-JUN-2016'; it does not has to be greater than current date (25-JUN-2014 would be wrong).

Thanks

Answer

In Oracle, you can use the next_day() function. It would look something like that:

select next_day(trunc(sysdate), 'MON') - 7

In other words, it gets the next Monday and then subtracts 7 days to get the previous one.

Comments