Get the First Weekday Occurrence In a Month For Any Date


 

DECLARE @ANYdATETIME datetime
SET @ANYdATETIME='20141113' 

select  DateAdd(day, (7+DATEPART(WEEKDAY,@ANYdATETIME) - DatePart(weekday, dateadd(month, datediff(MONTH, 0, @ANYdATETIME),0)))%7 ,dateadd(month, datediff(MONTH, 0, @ANYdATETIME),0) ) theFirstWeekdayOccuranceInaMonth

,dateadd(month, datediff(MONTH, 0,@ANYdATETIME),0)  as beginningofthemonth

 

You can use dateadd to get the second, third or fourth weekday:

 

DECLARE @ANYdATETIME datetime
SET @ANYdATETIME='20141113' 

select  DateAdd(day, (7+DATEPART(WEEKDAY,@ANYdATETIME) - DatePart(weekday, dateadd(month, datediff(MONTH, 0, @ANYdATETIME),0)))%7 ,dateadd(month, datediff(MONTH, 0, @ANYdATETIME),0) ) theFirstWeekdayOccuranceInaMonth

,dateadd(month, datediff(MONTH, 0,@ANYdATETIME),0)  as beginningofthemonth

, DateAdd(week,1
,DateAdd(day, (7+DATEPART(WEEKDAY,@ANYdATETIME) - DatePart(weekday, dateadd(month, datediff(MONTH, 0, @ANYdATETIME),0)))%7 ,dateadd(month, datediff(MONTH, 0, @ANYdATETIME),0) ))  as theSecondWeekdayOccuranceInaMonth
, DateAdd(week,2
,DateAdd(day, (7+DATEPART(WEEKDAY,@ANYdATETIME) - DatePart(weekday, dateadd(month, datediff(MONTH, 0, @ANYdATETIME),0)))%7 ,dateadd(month, datediff(MONTH, 0, @ANYdATETIME),0) )) as theThirdWeekdayOccuranceInaMonth
, DateAdd(week,3
,DateAdd(day, (7+DATEPART(WEEKDAY,@ANYdATETIME) - DatePart(weekday, dateadd(month, datediff(MONTH, 0, @ANYdATETIME),0)))%7 ,dateadd(month, datediff(MONTH, 0, @ANYdATETIME),0) )) as theFourthWeekdayOccuranceInaMonth
 

 
Advertisements


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s