Datepart query shows value 1 for saturday - sql

The following is my stored procedure for setting workingday between two dates
Create procedure [dbo].[sp_workingdays](#startdate date,#enddate date,#createddatetime datetime,#adminid int)
as
declare #start date,#end date
declare #day varchar(50)
declare #timeid int
declare #daypare int
declare #workingdaytype varchar(20)
begin
set #start=#startdate
set #end=#enddate
while (#start<=#end)
begin
if #start not in (select Date from Generalholyday_details)
begin
select #day= DATENAME(dw,#start)
select #workingdaytype =case (DATEPART(DW,#start)+##DATEFIRST)%7 when 1 then 'Leave Day' when 2 then 'Full Day' when 3 then 'Full Day' when 4 then 'Full Day'when 5 then 'Full Day' when 6 then 'Full Day' when 0 then 'Half Day' end
select #timeid=Time_id from Workingdaytimesetting_details where Workingday_type=#workingdaytype
insert into Workingday_details(Working_date,working_day,Time_id) values(#start,#day,#timeid)
update Workingday_details set createddatetime=#createddatetime where createddatetime is null
update Workingday_details set adminid=#adminid where adminid is null
end
set #start=DATEADD(day,1,#start)
end
end
GO
In datepart line 1 is for sunday. but actually when i run the stored procedure, i got 1 for Saturday. how can i set 1 for sunday.In my previous system i got 1 for sunday in same procedure.

Have a look at SET DATEFIRST (Transact-SQL)
Sets the first day of the week to a number from 1 through 7.
To see the current setting of SET DATEFIRST, use the ##DATEFIRST
function.
The setting of SET DATEFIRST is set at execute or run time and not at
parse time.
Specifying SET DATEFIRST has no effect on DATEDIFF. DATEDIFF always
uses Sunday as the first day of the week to ensure the function is
deterministic.

Try this one -
SET DATEFIRST 7
DATEFIRST - MSDN
Also try this query after small refactor:
CREATE PROCEDURE [dbo].[sp_workingdays]
(
#startdate DATE
, #enddate DATE
, #createddatetime DATETIME
, #adminid INT
)
AS BEGIN
DECLARE
#start DATE
, #end DATE
, #day VARCHAR(50)
, #timeid INT
, #workingdaytype VARCHAR(20)
SELECT
#start = #startdate
, #end = #enddate
WHILE (#start <= #end) BEGIN
IF NOT EXISTS(
SELECT 1
FROM Generalholyday_details
WHERE #start = [Date]
) BEGIN
SELECT
#day = DATENAME(dw, #start)
, #workingdaytype =
CASE WHEN dt = 1 THEN 'Leave Day'
WHEN dt IN (2,3,4,5,6) THEN 'Full Day'
ELSE 'Half Day'
END
FROM (
SELECT dt = (DATEPART(DW, #start) + ##DATEFIRST) % 7
) t
SELECT #timeid = Time_id
FROM Workingdaytimesetting_details
WHERE Workingday_type = #workingdaytype
INSERT INTO Workingday_details (Working_date, working_day, Time_id)
VALUES (#start, #day, #timeid)
UPDATE Workingday_details
SET createddatetime = #createddatetime
WHERE createddatetime IS NULL
UPDATE Workingday_details
SET adminid = #adminid
WHERE adminid IS NULL
END
SET #start = DATEADD(DAY, 1, #start)
END
END
GO

Related

How to add hours to work day in SQL?

I have seen many example adding working date (business days) to date in SQL. But I would like to add hour.
For example; I would like to add 36 hour to date not in Sunday , Saturday
Could one help me about it ?
CREATE FUNCTION AddWorkDays
(
#WorkingDays As Int,
#StartDate AS DateTime
)
RETURNS DateTime
AS
BEGIN
DECLARE #Count AS Int
DECLARE #i As Int
DECLARE #NewDate As DateTime
SET #Count = 0
SET #i = 0
WHILE (#i < #WorkingDays) --runs through the number of days to add
BEGIN
-- increments the count variable
SELECT #Count = #Count + 1
-- increments the i variable
SELECT #i = #i + 1
-- adds the count on to the StartDate and checks if this new date is a Saturday or Sunday
-- if it is a Saturday or Sunday it enters the nested while loop and increments the count variable
WHILE DATEPART(weekday,DATEADD(d, #Count, #StartDate)) IN (1,7)
BEGIN
SELECT #Count = #Count + 1
END
END
-- adds the eventual count on to the Start Date and returns the new date
SELECT #NewDate = DATEADD(d,#Count,#StartDate)
RETURN #NewDate
END
GO
The following will add N hours to a date (excluding Saturday and Sundays).
Example
Declare #Date1 datetime = '2017-04-28'
Declare #Hours int = 36
Select D=max(D)
From (
Select D,HN=-1+Row_Number() over (Order by D)
From (Select D=DateAdd(HOUR,-1+Row_Number() Over (Order By (select null)),#Date1) From master..spt_values n1 ) D
Where DateName(WEEKDAY,D) not in ('Saturday','Sunday')
) D1
Where HN=#Hours
Returns
2017-05-01 12:00:00.000
is it what you are looking for?
declare #num_hours int;
set #num_hours = 1;
select dateadd(HOUR, #num_hours, getdate()) as time_with_hour;
declare #num_hours int;
set #num_hours = 5;
select dateadd(HOUR, #num_hours, getdate()) as time_added,
getdate() as curr_date
This question is already answered Here

if condition which executes only on weekdays, i.e. mon-fri

I want to have a condition, which executes only on weekdays,
the variable #Executedate is int type and take only the day part of the datetime,if #Executeday is a saturday or sunday, it should be reset to monday.
Here is the piece of code,
Declare #Executeday int
declare #time varchar(200)
set #time = convert(varchar(8), getdate(), 108)
if day(getdate()) < 16
begin
set #Executeday = (select top 1 Description from cts_configuration(nolock) where name like 'ReminderMailerFirstFortNightDay')
end
else
begin
set #Executeday = (select top 1 Description from cts_configuration(nolock) where name like 'ReminderMailerSecondFortNightDay')
end
if (day(getdate())=#Executeday) or (day(getdate())=#Executeday+2) or (day(getdate())=#Executeday+4)
begin
if(#time between '10:00:00' and '11:00:00')
begin
I want the if condition here...(That resets #executeday to Monday, if it is a sat or sun)
DECLARE #ExecuteDay AS int = 7;
DECLARE #Today AS date = GETDATE();
DECLARE #ExecuteDate AS date = DATEADD(day, #ExecuteDay - DATEPART(day, #Today), #Today);
DECLARE #WeekDay AS int = (##DATEFIRST + DATEPART(WEEKDAY, #ExecuteDate)) % 7;
IF #WeekDay IN (0, 1)
SET #ExecuteDate = DATEADD(day, 2 - #WeekDay, #ExecuteDate)
PRINT #ExecuteDate

Finding Closest future date

Is it possible to find the closest future date (datetime) by a date varchar value?
Given,
DECLARE #DayValue VARCHAR(3)
, #DateValue DATETIME
SET #DayValue = 'Tue' -- Values could be 'Mon', 'Tue', 'Wed' and etc.
SET #DateValue = '10/15/2014' -- Format is MM/dd/yyyy
I want to get:
Oct 21 2014 12:00AM
Using Loop,
DECLARE #DayValue VARCHAR(3)
,#DateValue DATETIME
SET #DayValue = 'tue'
SET #DateValue = '10/15/2014'
declare #i int= 1 ,#day varchar(3) = null
while (#i<=7 )
begin
Select #day = left(datename (dw,#DateValue),3)
if #day = #DayValue
begin
Select #DateValue
break
end
Select #DateValue = #DateValue+ 1
Select #i = #i+1
end
You could use this function if you had a date-table:
CREATE FUNCTION [dbo].[GetNextDayOfWeek]
( #DayOfWeek VARCHAR(3),
#DateValue datetime
)
RETURNS SmallDateTime
AS
BEGIN
DECLARE #NextDayOfWeek smalldatetime
SET #NextDayOfWeek = (
SELECT
MIN(d.Date)
FROM
tDefDate d
WHERE
d.Date > #DateValue
AND LEFT(DATENAME(Weekday, d.Date), 3) = #DayOfWeek);
RETURN #NextDayOfWeek
END
Then it's simple as:
select [dbo].[GetNextDayOfWeek]('Tue', Getdate()) -- next tuesday=> 2014-10-21
Note that it takes the language of the database into account. So if it's in german:
select [dbo].[GetNextDayOfWeek]('Die', Getdate()) -- next tuesday(Dienstag)
Here's a version that works also without a date-table (but is less efficient).
CREATE FUNCTION [dbo].[GetNextDayOfWeek]
( #DayOfWeek VARCHAR(3),
#DateValue datetime
)
RETURNS SmallDateTime
AS
BEGIN
DECLARE #NextDayOfWeek smalldatetime
;WITH CTE as
(
SELECT GetDate() DateValue, DayNum=0
UNION ALL
SELECT DateValue + 1, DayNum=DayNum+1
FROM CTE
WHERE DayNum <=7
)
SELECT #NextDayOfWeek = (
SELECT
MIN(d.DateValue)
FROM
CTE d
WHERE d.DateValue > #DateValue
AND LEFT(DATENAME(Weekday, d.DateValue), 3) = #DayOfWeek
)OPTION (MAXRECURSION 8);
RETURN #NextDayOfWeek
END
If you could define DayValue as an integer, you solve this problem with more elegant way:
DECLARE #DayValue int, #DateValue DATETIME 
SET #DayValue = 3 -- Values could be 1-Sun, 2-Mon, 3-Tue, 4-Wed and etc. 
SET #DateValue = '10/15/2014' -- Format is MM/dd/yyyy
select dateadd(day,(7 + #DayValue - datepart(w,#DateValue)), #DateValue)
TRY SQL FIDDLE DEMO
No loops and will work in selects with multiple rows. :)
DECLARE #DayValue CHAR(3)
DECLARE #DateValue DATETIME
DECLARE #FutureDate DATE
SET #DayValue='MON'
SET #DateValue='10/12/2014'
DECLARE #Days TABLE
(
[DayOfWeek] TINYINT,
[DayValue] CHAR(3)
)
INSERT INTO #Days([DayOfWeek],[DayValue])
SELECT 0,'SUN' UNION
SELECT 1,'MON' UNION
SELECT 2,'TUE' UNION
SELECT 3,'WED' UNION
SELECT 4,'THU' UNION
SELECT 5,'FRI' UNION
SELECT 6,'SAT'
SET #FutureDate=
DATEADD(DAY,
--Skip to next week if we are already on the desired day or past it
+ CASE WHEN ((SELECT [DayOfWeek] FROM #Days WHERE [DayValue]=#DayValue)<DATEPART(WEEKDAY,#DateValue)) THEN 7 ELSE 0 END
--reset to start of week (add one as DATEPART is base 1, not base 0)
- DATEPART(WEEKDAY,#DateValue) + 1
--Add the desired day of the week
+ (SELECT [DayOfWeek] FROM #Days WHERE [DayValue]=#DayValue)
,#DateValue)
SELECT #FutureDate
This is a bit chunky solution, but it works. :)
SET DATEFIRST 1
DECLARE #DateValue DateTime
, #DayValue VARCHAR(3)
, #tmp INT
SET #DateValue = '09/30/2014'
SET #DayValue = 'wed'
SET #tmp = CASE #DayValue
WHEN 'Mon' THEN (1 - DATEPART(dw, #DateValue) + 7) % 7
WHEN 'Tue' THEN (2 - DATEPART(dw, #DateValue) + 7) % 7
WHEN 'Wed' THEN (3 - DATEPART(dw, #DateValue) + 7) % 7
WHEN 'Thu' THEN (4 - DATEPART(dw, #DateValue) + 7) % 7
WHEN 'Fri' THEN (5 - DATEPART(dw, #DateValue) + 7) % 7
WHEN 'Sat' THEN (6 - DATEPART(dw, #DateValue) + 7) % 7
WHEN 'Sun' THEN (7 - DATEPART(dw, #DateValue) + 7) % 7
END
SELECT
CASE
WHEN #tmp = 0 THEN DATEADD (DAY, 7, #DateValue)
ELSE DATEADD (DAY, #tmp, #DateValue)
END

get DATEDIFF excluding weekends using sql server

I am using this query to get time taken.
SELECT DATEDIFF(dd, ActualStartDate, ActualCompletionDate) AS TimeTaken
FROM TableName
Now I want to exclude weekends and only include Mon-Fri as days counted.
Example query below, here are some details on how I solved it.
Using DATEDIFF(WK, ...) will give us the number of weeks between the 2 dates. SQL Server evaluates this as a difference between week numbers rather than based on the number of days. This is perfect, since we can use this to determine how many weekends passed between the dates.
So we can multiple that value by 2 to get the number of weekend days that occurred and subtract that from the DATEDIFF(dd, ...) to get the number of weekdays.
This doesn't behave 100% correctly when the start or end date falls on Sunday, though. So I added in some case logic at the end of the calculation to handle those instances.
You may also want to consider whether or not the DATEDIFF should be fully inclusive. e.g. Is the difference between 9/10 and 9/11 1 day or 2 days? If the latter, you'll want to add 1 to the final product.
declare #d1 datetime, #d2 datetime
select #d1 = '9/9/2011', #d2 = '9/18/2011'
select datediff(dd, #d1, #d2) - (datediff(wk, #d1, #d2) * 2) -
case when datepart(dw, #d1) = 1 then 1 else 0 end +
case when datepart(dw, #d2) = 1 then 1 else 0 end
I found when i used this there was a problem when d1 fell on saturday. Below is what i used to correct this.
declare #d1 datetime, #d2 datetime
select #d1 = '11/19/2011' , #d2 = '11/28/2011'
select datediff(dd, #d1, #d2) +case when datepart(dw, #d1) = 7 then 1 else 0 end - (datediff(wk, #d1, #d2) * 2) -
case when datepart(dw, #d1) = 1 then 1 else 0 end +
case when datepart(dw, #d2) = 1 then 1 else 0 end
BEGIN
DECLARE #totaldays INT;
DECLARE #weekenddays INT;
SET #totaldays = DATEDIFF(DAY, #startDate, #endDate)
SET #weekenddays = ((DATEDIFF(WEEK, #startDate, #endDate) * 2) + -- get the number of weekend days in between
CASE WHEN DATEPART(WEEKDAY, #startDate) = 1 THEN 1 ELSE 0 END + -- if selection was Sunday, won't add to weekends
CASE WHEN DATEPART(WEEKDAY, #endDate) = 6 THEN 1 ELSE 0 END) -- if selection was Saturday, won't add to weekends
Return (#totaldays - #weekenddays)
END
This is on SQL Server 2014
declare #d1 datetime, #d2 datetime
select #d1 = '4/19/2017', #d2 = '5/7/2017'
DECLARE #Counter int = datediff(DAY,#d1 ,#d2 )
DECLARE #C int = 0
DECLARE #SUM int = 0
WHILE #Counter > 0
begin
SET #SUM = #SUM + IIF(DATENAME(dw,
DATEADD(day,#c,#d1))IN('Sunday','Monday','Tuesday','Wednesday','Thursday')
,1,0)
SET #Counter = #Counter - 1
set #c = #c +1
end
select #Sum
If you hate CASE statements as much as I do, and want to be able to use the solution inline in your queries, just get the difference of days and subtract the count of weekend days and you'll have the desired result:
declare #d1 datetime, #d2 datetime, #days int
select #d1 = '2018/10/01', #d2 = '2018/11/01'
SET #days = DateDiff(dd, #d1, #d2) - DateDiff(ww, #d1, #d2)*2
print #days
(The only caveat--or at least point to keep in mind--is that this calculation is not inclusive of the last date, so you might need to add one day to the end date to achieve an inclusive result)
I just want to share the code I created that might help you.
DECLARE #MyCounter int = 0, #TempDate datetime, #EndDate datetime;
SET #TempDate = DATEADD(d,1,'2017-5-27')
SET #EndDate = '2017-6-3'
WHILE #TempDate <= #EndDate
BEGIN
IF DATENAME(DW,#TempDate) = 'Sunday' OR DATENAME(DW,#TempDate) = 'Saturday'
SET #MyCounter = #MyCounter
ELSE IF #TempDate not in ('2017-1-1', '2017-1-16', '2017-2-20', '2017-5-29', '2017-7-4', '2017-9-4', '2017-10-9', '2017-11-11', '2017-12-25')
SET #MyCounter = #MyCounter + 1
SET #TempDate = DATEADD(d,1,#TempDate)
CONTINUE
END
PRINT #MyCounter
PRINT #TempDate
If you do have a holiday table, you can also use that so that you don't have to list all the holidays in the ELSE IF section of the code. You can also create a function for this code and use the function whenever you need it in your query.
I hope this might help too.
Using https://stackoverflow.com/a/1804095 and JeffFisher30's answer above (https://stackoverflow.com/a/14572370/6147425) and my own need to have fractional days, I wrote this:
DateDiff(second,Start_Time,End_Time)/86400.0
-2*DateDiff(week, Start_Time, End_Time)
-Case When (DatePart(weekday, Start_Time)+##DateFirst)%7 = 1 Then 1 Else 0 End
+Case When (DatePart(weekday, End_Time)+##DateFirst)%7 = 1 Then 1 Else 0 End
Use this function to calculate the number of business days excluding Saturday and Sunday. Also it will exclude start date and it will include end date.
-- Select [dbo].[GetBussinessDays] ('02/18/2021', '03/06/2021') -- 11 days
CREATE or ALTER FUNCTION [dbo].[GetBussinessDays] (
#StartDate DATETIME,
#EndDate DATETIME
)
returns INT AS
BEGIN
DECLARE #tempStartDate DATETIME= #StartDate;
DECLARE #tempEndDate DATETIME = #EndDate;
IF(#tempStartDate IS NULL
OR
#tempEndDate IS NULL)
BEGIN
RETURN NULL;
END
--To avoid negative values reverse the date if StartDate is grater than EndDate
IF(#StartDate > #EndDate)
BEGIN
SET #StartDate = #tempEndDate;
SET #EndDate = #tempStartDate;
END
DECLARE #Counter INT = Datediff(day,#StartDate ,#EndDate);
DECLARE #TempCounter INT = 0;
DECLARE #TotalBusinessDays INT = 0;
WHILE #Counter >= 0
BEGIN
IF(#TempCounter > 0 OR #Counter = 1) -- To ignore first day's calculation
Begin
SET #TotalBusinessDays = #TotalBusinessDays + Iif(Datename(dw, Dateadd(day,#TempCounter,#StartDate)) IN('Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday'),1,0)
END
SET #Counter = #Counter - 1
SET #TempCounter = #TempCounter +1
END
RETURN #TotalBusinessDays;
END
Using #Derek Kromm answer (Current Marked Answer)
I have modified so it IS tolerant of any localisations that may be on the target server.
DECLARE #d1 DATETIME, #d2 DATETIME
SELECT #d1 = '10/01/2022', #d2 = '10/28/2022'
SELECT (datediff(dd, #d1, #EndQuery)+1) - (datediff(wk, #d1, dateadd(dd,1,#d2)) * 2)
- CASE WHEN DateName(WEEKDAY, #d1) = 'Sunday' THEN 1 ELSE 0 END -- This includes for start date edge case
+ CASE WHEN DateName(WEEKDAY, #d2) = 'Saturday' THEN 1 ELSE 0 END -- This includes for end date edge case.
This is with the end date being innclusive.
/*
EXAMPLE:
/MONDAY/
SET DATEFIRST 1
SELECT dbo.FUNC_GETDATEDIFFERENCE_WO_WEEKEND('2019-02-01','2019-02-12')
*/
CREATE FUNCTION FUNC_GETDATEDIFFERENCE_WO_WEEKEND
(
#pdtmaLastLoanPayDate DATETIME,
#pdtmaDisbursedDate DATETIME
)
RETURNS BIGINT
BEGIN
DECLARE
#mintDaysDifference BIGINT
SET #mintDaysDifference = 0
WHILE CONVERT(NCHAR(10),#pdtmaLastLoanPayDate,121) <= CONVERT(NCHAR(10),#pdtmaDisbursedDate,121)
BEGIN
IF DATEPART(WEEKDAY,#pdtmaLastLoanPayDate) NOT IN (6,7)
BEGIN
SET #mintDaysDifference = #mintDaysDifference + 1
END
SET #pdtmaLastLoanPayDate = DATEADD(DAY,1,#pdtmaLastLoanPayDate)
END
RETURN ISNULL(#mintDaysDifference,0)
END

SQL Nth Day of Nth Week of a Month

I want to use the following function for scheduling club meetings which occur on a monthly basis based on the week and weekday of the month. In the example below I have a (to be) function that returns the Third Wednesday of the month. If that day occurs in the past then it returns the next month's 3rd Wednesday.
I want to get away from the Loops and I feel that there is a better method for calculation. Is there a more OO process? Your opinion?
--CREATE FUNCTION NextWeekDayofMonth
DECLARE
--(
#WEEK INT,
#WEEKDAY INT,
#REFERENCEDATE DATETIME
--)
--RETURNS DATETIME
--AS
-------------------------------
--Values for testing - Third Wednesday of the Month
set #WEEK = 3 --Third Week
set #WEEKDAY = 4 --Wednesday
set #REFERENCEDATE = '08/20/2011'
-------------------------------
BEGIN
DECLARE #WEEKSEARCH INT
DECLARE #FDOM DATETIME
DECLARE #RETURNDATE DATETIME
SET #FDOM = DATEADD(M,DATEDIFF(M,0,#REFERENCEDATE),0)
SET #RETURNDATE = DATEADD(M,0,#FDOM)
WHILE (#RETURNDATE < #REFERENCEDATE)
--If the calculated date occurs in the past then it
--finds the appropriate date in the next month
BEGIN
SET #WEEKSEARCH = 1
SET #RETURNDATE = #FDOM
--Finds the first weekday of the month that matches the provided weekday value
WHILE ( DATEPART(DW,#RETURNDATE) <> #WEEKDAY)
BEGIN
SET #RETURNDATE = DATEADD(D,1,#RETURNDATE)
END
--Iterates through the weeks without going into next month
WHILE #WEEKSEARCH < #WEEK
BEGIN
IF MONTH(DATEADD(WK,1,#RETURNDATE)) = MONTH(#FDOM)
BEGIN
SET #RETURNDATE = DATEADD(WK,1,#RETURNDATE)
SET #WEEKSEARCH = #WEEKSEARCH+1
END
ELSE
BREAK
END
SET #FDOM = DATEADD(M,1,#FDOM)
END
--RETURN #RETURNDATE
select #ReturnDate
END
IMO, the best process is to store important business information as rows in tables in a database. If you build a calendar table, you can get all the third Wednesdays by a simple query. Not only are the queries simple, they can be seen to be obviously correct.
select cal_date
from calendar
where day_of_week_ordinal = 3
and day_of_week = 'Wed';
The third Wednesday that's on or after today is also simple.
select min(cal_date)
from calendar
where day_of_week_ordinal = 3
and day_of_week = 'Wed'
and cal_date >= CURRENT_DATE;
Creating a calendar table is straightforward. This was written for PostgreSQL, but it's entirely standard SQL (I think) except for the columns relating to ISO years and ISO weeks.
create table calendar (
cal_date date primary key,
year_of_date integer not null
check (year_of_date = extract(year from cal_date)),
month_of_year integer not null
check (month_of_year = extract(month from cal_date)),
day_of_month integer not null
check (day_of_month = extract(day from cal_date)),
day_of_week char(3) not null
check (day_of_week =
case when extract(dow from cal_date) = 0 then 'Sun'
when extract(dow from cal_date) = 1 then 'Mon'
when extract(dow from cal_date) = 2 then 'Tue'
when extract(dow from cal_date) = 3 then 'Wed'
when extract(dow from cal_date) = 4 then 'Thu'
when extract(dow from cal_date) = 5 then 'Fri'
when extract(dow from cal_date) = 6 then 'Sat'
end),
day_of_week_ordinal integer not null
check (day_of_week_ordinal =
case
when day_of_month >= 1 and day_of_month <= 7 then 1
when day_of_month >= 8 and day_of_month <= 14 then 2
when day_of_month >= 15 and day_of_month <= 21 then 3
when day_of_month >= 22 and day_of_month <= 28 then 4
else 5
end),
iso_year integer not null
check (iso_year = extract(isoyear from cal_date)),
iso_week integer not null
check (iso_week = extract(week from cal_date))
);
You can populate that table with a spreadsheet or with a UDF. Spreadsheets usually have pretty good date and time functions. I have a UDF, but it's written for PostgreSQL (PL/PGSQL), so I'm not sure how much it would help you. But I'll post it later if you like.
Here's a date math way to accomplish what you want without looping:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Description: Gets the nth occurrence of a given weekday in the month containing the specified date.
-- For #dayOfWeek, 1 = Sunday, 2 = Monday, 3 = Tuesday, 4 = Wednesday, 5 = Thursday, 6 = Friday, 7 = Saturday
-- =============================================
CREATE FUNCTION GetWeekdayInMonth
(
#date datetime,
#dayOfWeek int,
#nthWeekdayInMonth int
)
RETURNS datetime
AS
BEGIN
DECLARE #beginMonth datetime
DECLARE #offSet int
DECLARE #firstWeekdayOfMonth datetime
DECLARE #result datetime
SET #beginMonth = DATEADD(DAY, -DATEPART(DAY, #date) + 1, #date)
SET #offSet = #dayOfWeek - DATEPART(dw, #beginMonth)
IF (#offSet < 0)
BEGIN
SET #firstWeekdayOfMonth = DATEADD(d, 7 + #offSet, #beginMonth)
END
ELSE
BEGIN
SET #firstWeekdayOfMonth = DATEADD(d, #offSet, #beginMonth)
END
SET #result = DATEADD(WEEK, #nthWeekdayInMonth - 1, #firstWeekdayOfMonth)
IF (NOT(MONTH(#beginMonth) = MONTH(#result)))
BEGIN
SET #result = NULL
END
RETURN #result
END
GO
DECLARE #nextMeetingDate datetime
SET #nextMeetingDate = dbo.GetWeekdayInMonth(GETDATE(), 4, 3)
IF (#nextMeetingDate IS NULL OR #nextMeetingDate < GETDATE())
BEGIN
SET #nextMeetingDate = dbo.GetWeekDayInMonth(DATEADD(MONTH, 1, GETDATE()), 4, 3)
END
SELECT #nextMeetingDate
Here's another function based solution using date math that returns the Next Nth Weekday on or after a given date. It doesn't use any looping to speak of, but it may recurs by at most one iteration if the next Nth weekday is in the next month.
This function takes the DATEFIRST setting into account for environments that use a value other than the default.
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: David Grimberg
-- Create date: 2015-06-18
-- Description: Gets the next Nth weekday
-- #param Date is any date in a month
-- #param DayOfWeek is the weekday of interest ranging
-- from 1 to 7 with ##DATEFIRST being the
-- first day of the week
-- #param NthWeekday represents which ordinal weekday to return.
-- Positive values return dates relative to the start
-- of the month. Negative values return dates relative
-- to the end of the month. Values > 4 indicate the
-- last week, values < -4 indicate the first week.
-- Zero is assumed to be 1.
-- =============================================
ALTER FUNCTION dbo.xxGetNextNthWeekday
(
#Date date,
#NthWeekday smallint,
#DayOfWeek tinyint
)
RETURNS date
AS
BEGIN
DECLARE #FirstOfMonth date
DECLARE #inc int
DECLARE #Result date
-- Clamp the #NthWeekday input to valid values
set #NthWeekday = case when #NthWeekday = 0 then 1
when #NthWeekday > 4 then -1
when #NthWeekday < -4 then 1
else #NthWeekday
end
-- Normalize the requested day of week taking
-- ##DATEFIRST into consideration.
set #DayOfWeek = (##DATEFIRST + 6 + #DayOfWeek) % 7 + 1
-- Gets the first of the current month or the
-- next month if #NthWeekday is negative.
set #FirstOfMonth = dateadd(month, datediff(month,0,#Date)
+ case when #NthWeekday < 0 then 1 else 0 end
, 0)
-- Add and/or subtract 1 week depending direction of search and the
-- relationship of #FirstOfMonth's Day of the Week to the #DayOfWeek
-- of interest
set #inc = case when (datepart(WEEKDAY, #FirstOfMonth)+##DATEFIRST-1)%7+1 > #DayOfWeek
then 0
else -1
end
+ case when #NthWeekday < 0 then 1 else 0 end
-- Put it all together
set #Result = dateadd( day
, #DayOfWeek-1
, dateadd( WEEK
, #NthWeekday + #inc
, dateadd( WEEK -- Gets 1st Sunday on or
, datediff(WEEK, -1, #FirstOfMonth)
,-1 ) ) ) -- before #FirstOfMonth
-- [Snip here] --
if #Result < #Date
set #Result = dbo.xxGetNextNthWeekday( dateadd(month, datediff(month, 0, #Date)+1, 0)
, #NthWeekday, #DayOfWeek)
-- [to here for no recursion] --
Return #Result
END
If you want the past or future Nth weekday of the current month based on the #Date parameter rather than the next Nth weekday snip out the recursive part as indicated above.