Today I needed a PL/SQL funtion to determine next friday’s date.
Therefore I had to write a strange PL/SQL function. I’m kind of mixed up – am I complicated?
I’m sure there’s a pretty simple Oracle solution out there – hide out!
Hopefully Steven Feuerstein never sees this lines of code. Maybe someon has a better idea than this ….
declare /* Calculate next friday, if today is friday then return today */ function NextFriday return date is vNextFriday date; vNext date; vFR date; vMO date; begin SELECT trunc(NEXT_DAY(SYSDATE, 'FRIDAY' /*'FREITAG' */)) into vNextFriday FROM dual; select trunc(sysdate)+7 into vNext from dual; if vNextFriday = vNext then select trunc(sysdate) into vFR from dual; else select trunc(NEXT_DAY(SYSDATE, 'FRIDAY')) into vFR from dual; end if; -- Monday vMO := vFR-4; -- Friday return vFR end NextFriday; begin dbms_output.put_line('Next Friday: ' ||NextFriday); end;
I’m looking for code to determine the “2nd Friday of the month” and I found this:
select next_day(sysdate,’FRIDAY’) as NEXT_DAY from dual
however, this prints the next Friday, not the 2nd Friday.
Thought I’d share.
SELECT DATE(DATE_ADD(‘2010-05-21’, INTERVAL 6 – DAYOFWEEK(‘2010-05-21’) DAY))
there is a small mistake, because saturdays are considered same week. solution is to add 1 day to the date you wanna check:
SELECT
DATE(
DATE_ADD(
‘2010-05-19’ + INTERVAL 1 DAY,
INTERVAL 6 – DAYOFWEEK(‘2010-05-19’ + INTERVAL 1 DAY) DAY
)
)