Writing Oracle NEXT_DAY Function in SQL Server

Hello everyone,

In this article, I will give information about writing the Oracle NEXT_DAY function in SQL Server.

In SQL Server, in some cases, we may want to see and use functions that exist in other databases.

What is the NEXT_DAY function?

The NEXT_DAY function has 2 parameters. The first parameter is used to keep the date information, and the second parameter is used to keep the day information of the date we want to see first after the date we specify.

You can easily do this with the help of the function below.

CREATE FUNCTION dbo.SQL_Server_Next_Day
(
    @FromDate DATETIME,
    @DayNumber INT
)
RETURNS DATETIME
AS
BEGIN

    DECLARE @CurrentDate AS DATETIME;
    DECLARE @DayOfWeek AS TINYINT;

    SELECT @DayOfWeek = DATEPART(WEEKDAY, @FromDate);
    SELECT @DayNumber = @DayNumber + 1;

    IF (@DayOfWeek < @DayNumber)
    BEGIN
        SELECT @CurrentDate = (@FromDate + (@DayNumber - @DayOfWeek));
    END;
    ELSE
    BEGIN
        SELECT @CurrentDate = (@FromDate + ((@DayNumber + 2) - (@DayOfWeek - @DayNumber)));
    END;

    RETURN @CurrentDate;
END;

GO

--Use of the function

SELECT dbo.SQL_Server_Next_Day(GETDATE(),5)

When you run the function, you will see a result like the one below.

Writing Oracle NEXT DAY Function in SQL Server

As you can see, after the date we stated, we saw the day we wanted to see for the first time.

Good luck to everyone in business and life.

89 Views

Yavuz Selim Kart

I try to explain what I know in software and database. I am still improving myself by doing research on many programming languages. Apart from these, I am also interested in Graphic Design and Wordpress. I also have knowledge about SEO and Social media management. In short, I am a determined person who likes to work hard.

You may also like...

Don`t copy text!