Search This Blog

Thursday, September 29, 2011

Find 2nd Monday Of The Month

Hi,

Recently one of our fellow DBA friend asked a question on MSDN. Question was, he want to have a job with 2 steps, First step will run everyday and pass on the information but second step should only get executed on 2nd Monday of the Month.

Now how to do that, ofcourse he has to put some conditions to check whether its 2nd Monday of the Month or not and then process code accordingly. So question is how to find 2nd Monday of the Month. Here is the code friends.

DECLARE @DAY VARCHAR(10)


DECLARE @DATE VARCHAR(10)

DECLARE @TODAY VARCHAR(10)


SET @DAY='Monday'

SET @DATE=CONVERT(varchar(10),DATEADD(wk, DATEDIFF(wk,0, dateadd(dd,14-datepart(day,getdate()),getdate())), 0),111)
SET @TODAY=CONVERT(VARCHAR(10),getdate(),111)
IF @DAY= DATENAME(WEEKDAY,DATEADD(wk, DATEDIFF(wk,0, dateadd(dd,14-datepart(day,getdate()),getdate())), 0))

AND @TODAY=@DATE

BEGIN

SELECT 'Its Second '+@DAY+' Of The Month And Date Is '+@TODAY

END

ELSE

SELECT 'Its '+@DAY+ ' Today and Date Is '+@TODAY
 
Hope this will help.

1 comment:

Anonymous said...

I tried this but it was not working correctly, it is 25th april and it is Thursday but it shows as monday. Anyway got idea.