SERPland Blog

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

· 875 words · 5 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.


2025 Anleitungs-Beschreibung (Instruction Manual)

Calculate next this Friday date with PLSQL 🔗

In today’s tutorial, we will explore how to calculate the next Friday date using PLSQL. This is a common requirement in many applications, and we will show you a simple and efficient way to achieve this using Oracle’s built-in functions.

The Challenge 🔗

When faced with the task of determining the next Friday date, developers often resort to writing custom PLSQL functions that can be complex and difficult to maintain. In the past, this process involved checking if today is Friday and then calculating the next occurrence of Friday based on the current date.

The Solution 🔗

Thanks to advancements in Oracle PLSQL, there is now a simpler solution to calculate the next Friday date. By leveraging the NEXTDAY function, developers can efficiently find the next occurrence of a specific day of the week after a given date. This eliminates the need for complicated logic in custom functions and streamlines the process.

Below is an example of how you can use the NEXTDAY function to calculate the next Friday date:

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

With this simplified function, you no longer need to check if today is Friday or calculate the next Friday date based on the current date. The NEXTDAY function handles all these checks internally, making the code concise and easy to understand.

2025 Perspective 🔗

As of the year 2025, developers continue to benefit from the advancements in Oracle PLSQL, particularly the NEXTDAY function for date calculations. This functionality has become a standard practice in Oracle development, leading to more efficient and maintainable code. By embracing built-in functions like NEXTDAY, developers can enhance their productivity and produce high-quality code.

Fazit 2025 🔗

In conclusion, the evolution of Oracle PLSQL has made calculating the next Friday date simpler and more efficient. Developers now have access to powerful built-in functions that streamline date calculations and improve code readability. By staying updated on the latest features and best practices in PLSQL development, developers can elevate their skills and deliver exceptional solutions.

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