0

I need to run a job every day 16. But if it is "saturday" or "sunday" it must run in the next business day.

I don't care if it is holidays or not. I just need to run it.

I could do something like this and separete it on steps, and only pass to the next step if it runs with no error:

--Step 1 - Verify if it is 16
IF  day(getdate()) <> 16
    RAISERROR ('Job runs only day 16.',16,1) 

--Step 2 - verify if its saturday of sunday
IF  DATEPART(weekday,getdate()) IN ( 7,1 )
    RAISERROR ('Job will run on the next business day.',16,1)

I would create 2 steps with these queries, but this is not going to work since, lets say the next business day is 18, the first step will raise an error.

Is there a way to schedule something like this?

3

It's not beautiful, but you could wrap all of your conditions into a CASE like so:

DECLARE @IsGood bit;
SET @IsGood = SELECT CASE WHEN DAY(GETDATE()) = 16 AND DATEPART(WEEKDAY, GETDATE()) NOT IN (1, 7) THEN 1
                          WHEN DAY(GETDATE()) IN (17, 18) AND DATEPART(WEEKDAY, GETDATE()) = 2 THEN 1
                          ELSE 0 END;

IF @IsGood <> 1
    RAISERROR ('Nope. Not today.', 16, 1); 

Throw that into a scheduled job that runs on the 16th, 17th, and 18th of every month, and you'll be good.

1
  • Thanks man. I was rolling on the floor thinking about a solution. it's gonna work. thanks bro. gonna run it every workday, I dont think it needs to run on 16 17 and 18 day. this way I would need to create 3 jobs to run on 3 different days.
    – Racer SQL
    May 19 '21 at 15:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.