Writing Oracle NEXT_DAY Function in SQL Server
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.
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.