Syam asked a while ago how we could find something like the first Monday or third Saturday in a month. Well Syam, it’s taken me a while to respond but here we go.
For this example we’ll use
sysdate as input but any Oracle date will work. You can also substitute any other day of the week for Monday.
The first day of the month is probably a good place to start:
SQL> select sysdate from dual;
SQL> select trunc(sysdate, 'MONTH') FROM DUAL;
Now that we’ve got that we can find the first Monday with the
next_day function. Of course we need to remember the
next_day function looks for the next named day after the date provided so we subtract 1 day from the date in case the first is a Monday.
SQL> select next_day(trunc(sysdate, 'MONTH')-1, 'Monday') from dual;
Now that we have the first Monday of the month we can add 7 days to find the second Monday or 14 to find the third.
SQL> select next_day(trunc(sysdate, 'MONTH')-1, 'Monday')+7 FROM dual;
SQL> select next_day(trunc(sysdate, 'MONTH')-1, 'Monday')+14 FROM dual;
So from here you can change the day you’re looking for or the week number you want it in.
oracle, date functions, sql, database, database development, pl/sql