SERPland Blog

Calculate next (this) friday date with pl/sql - hide out!

· 500 words · 3 minutes to read

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;


Update 2024

Update on Calculating Next Friday Date with PL/SQL 🔗

In the year 2011, the author expressed the need for a PL/SQL function to determine the next Friday’s date. The function provided in the text seemed a bit convoluted, and the author hoped for a simpler Oracle solution. As of the year 2024, the need to calculate the next Friday date remains relevant, but there have been advancements in Oracle PL/SQL that can simplify the process.

One of the improvements in Oracle PL/SQL over the years is the introduction of built-in functions that make date calculations more straightforward. For example, the NEXT_DAY function can be used to find the next occurrence of a specific day of the week after a given date. This eliminates the need for complex logic in custom functions.

Here is an updated version of the PL/SQL function to calculate the next Friday’s date using the NEXT_DAY function:

FUNCTION NextFriday RETURN DATE IS
    vNextFriday DATE;
BEGIN
    SELECT NEXT_DAY(TRUNC(SYSDATE), 'FRIDAY') INTO vNextFriday FROM dual;
    
    RETURN vNextFriday;
END NextFriday;

With this simplified function, there is no need for conditional logic to check if today is Friday or to calculate the next Friday based on the current date. The NEXT_DAY function handles these checks internally, making the code more concise and easier to understand.

As of the year 2024, developers working with Oracle PL/SQL can leverage these built-in functions to streamline date calculations and improve code readability. It is essential to stay updated on the latest features and best practices in PL/SQL development to write efficient and maintainable code.

In conclusion, while the need to calculate the next Friday date remains constant, advancements in Oracle PL/SQL have made the process simpler and more efficient. By utilizing built-in functions like NEXT_DAY, developers can achieve the desired outcome with fewer lines of code and greater clarity.

Don’t hide out anymore in complex custom functions – embrace the power of Oracle’s built-in functions for your date calculations!

2024 Update: The advancements in Oracle PL/SQL, such as the NEXT_DAY function, have made calculating the next Friday date even easier and more efficient. Developers can now rely on these built-in functions for streamlined date calculations.