HiveBrain v1.2.0
Get Started
← Back to all entries
snippetsqlMinor

Can I create a SQL Job to run on specified dates?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
cancreatesqlspecifieddatesjobrun

Problem

I want to create a new SQL job that runs the beginning of every quarter:
01/01
04/01
07/01
10/01
Is this possible? I know i can set it to run every 90 days but that's not exact. I am using SQL Server 2008. Thanks in advance!

Solution

You can't do this using the point-and-click UI, because it is primitive and inflexible, but you can set the job to run once a month, on the first, and put this in your job step:

IF DATEPART(MONTH, GETDATE()) IN (1,4,7,10)
BEGIN
  -- DO THE THINGS!
END


If you have multiple steps and want the whole job to exit, rather than having to put that logic in each step, you can reverse the condition and raise an error, and set the step properties to quit the job on error. If you want more graceful failure, you could add a step at the end that quits the job with success, and make that the next step for the first step on error.

IF DATEPART(MONTH, GETDATE()) NOT IN (1,4,7,10)
BEGIN
  RAISERROR('This is not the beginning of a quarter, silly.',11,1);
END

Code Snippets

IF DATEPART(MONTH, GETDATE()) IN (1,4,7,10)
BEGIN
  -- DO THE THINGS!
END
IF DATEPART(MONTH, GETDATE()) NOT IN (1,4,7,10)
BEGIN
  RAISERROR('This is not the beginning of a quarter, silly.',11,1);
END

Context

StackExchange Database Administrators Q#115694, answer score: 3

Revisions (0)

No revisions yet.