Find the second work day of the month

Just some fun with T-SQL. :)
Recently someone asked me to write a code which answers the question if today is the second business day of the month or not.
Here is a version of the code. If you have better ideas for doing this, feel free to post a comment.

 

declare @SecondDayOfMonth datetime
declare @Year int
declare @month int
set @Year = datepart(yy,getdate())
set @month = datepart(mm,getdate())
 
set @SecondDayOfMonth = convert(datetime, convert(varchar,@Year)+'-'+convert(varchar,@month)+'-02')
 
-- if the 2nd day of the month is not Saturday, Sunday or Monday, then it is the second work day
if datepart(weekday,@SecondDayOfMonth) not in (1,2,7)
      select @SecondDayOfMonth = (@SecondDayOfMonth)
 
-- if the 2nd day of the month is Saturday or Sunday, then get 2 days later
if datepart(weekday,@SecondDayOfMonth) in (1,7)
	 select @SecondDayOfMonth =(dateadd(day,2,@SecondDayOfMonth))
 
-- if the 2nd day of the month is Monday, then get the next day
if datepart(weekday,@SecondDayOfMonth) = 2
	 select @SecondDayOfMonth =(dateadd(day,1,@SecondDayOfMonth))	
 
 select case convert(datetime,@SecondDayOfMonth)
	when convert(datetime, convert(varchar,datepart(yy,getdate()))+'-'+convert(varchar,datepart(mm,getdate()))+'-'+convert(varchar,datepart(dd,getdate())))
	then 'True'
	else 'False'
	end
	as TodayIsTheSecondBusinessDayOfThisMonth

 

Comments are closed.