Consider this example scenario. We have a SSIS package which needs to be executed during first day of every quarter of the year. If you see the standard schedules available in SQL agent job, you can see that there is no provision to do this directly as quarter is not one of the options available as per the below screenshot.
In such cases we need to apply an approach as below
Add a new step at the beginning of the job. The purpose of this step would be to check if its a valid day for the job execution. In the above case valid days are quarter start dates.For all the other days we dont need to execute the core steps. The step type would be Transact SQL script and we will use a code as below
IF DATEADD(dd,DATEDIFF(dd,0,GETDATE()),0) <> DATEADD(qq,DATEDIFF(qq,0,GETDATE()),0)
RAISERROR (‘Job cant be executed on this day’,16,1)
What the above code snippet does is to check if current date represents the start date of a quarter. If not, it will throw an error stating that you cant execute job on that day.
The logic for quarter start, current day etc are explained here
http://visakhm.blogspot.in/2010/01/some-quick-tips-for-date-formating.html
Now we configure the job step settings as below for the new step
We configure failure action as quit reporting success for the first step.This will make sure the job will quit reporting success without executing the core steps on the non scheduled days.
Now try executing the job on quarter start and on another day and check the job execution history
The history would look like below for a quarter start day
IF dbo.GetWeekDayNumberOfMonth(@dt) = 1 AND DATEDIFF(dd,0,@dt) % 7 = 4
SELECT 1
ELSE
RAISERROR (‘Job cant be executed on this day’,16,1)