The following scalar valued function will return the next working day of the week. Here is the code:
CREATE FUNCTION dbo.udf_GetNextWorkingDay (@p_Date DATETIME )
RETURNS DATETIME
AS
BEGIN
DECLARE @m_WeekDay INT
DECLARE @rt_Next_Working_Day DATETIME
SET @m_WeekDay = DATEPART(weekday,@p_Date)
IF @m_WeekDay = 6 -- Friday
SET @rt_Next_Working_Day = DATEADD(d,3,@p_Date)
ELSE IF @m_WeekDay = 7 -- Saturday
SET @rt_Next_Working_Day = DATEADD(d,2,@p_Date)
ELSE
SET @rt_Next_Working_Day = DATEADD(d,1,@p_Date)
RETURN @rt_Next_Working_Day
END
To test this UDF you can use the following example:
SELECT dbo.udf_GetNextWorkingDay ( '07/20/2007')
SELECT dbo.udf_GetNextWorkingDay ( '07/21/2007')
SELECT dbo.udf_GetNextWorkingDay ( '07/22/2007')
The result of all of the above test cases will be as following:
2007-07-23 00:00:00.000
No comments:
Post a Comment