Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Skipping the weekend

Author  Topic 

divan
Posting Yak Master

153 Posts

Posted - 2012-10-30 : 14:42:17
How do I write a script that will not run a job on a weekend that is Saturday and Sunday.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-10-30 : 15:08:19
add a condition like

WHERE DATEDIFF(dd,0,GETDATE())%7 < 5

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

divan
Posting Yak Master

153 Posts

Posted - 2012-10-31 : 08:09:09
Good Morning Visakh16.. Can you please tell me what %7 in the condition mean .... Thanks
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-10-31 : 09:00:56
% is the modulo operator - it returns there remainder if left operand is divided by the right. E.g. 12%7 = 5

The logic of what Visakh was doing is as follows:

Datediff function counts the number of days from the the second operand (0, which is Jan 1, 1900 - which was a Monday) to the current date.

So:

if today is also a Monday, the difference between 1/1/1900 and today will be an exact multiple of 7, so the remainder will be zero.
if today is a Tuesday,the difference will be a multiple of 7 plus 1, so the remainder will be 1
and so on...
if today is a Saturday,the difference will be a multiple of 7 plus 5, so the remainder will be 5
if today is a Sunday,the difference will be a multiple of 7 plus 6, so the remainder will be 6

So, if the remainder is less than 5, it is Monday-Friday, i.e., weekday.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-10-31 : 09:20:42
And I prefer to do it that way as its independent of any datefirst and regional settings

http://visakhm.blogspot.com/2012/08/creating-server-independent-day.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

divan
Posting Yak Master

153 Posts

Posted - 2012-10-31 : 11:30:43
Thank you for the explanation this makes a lot of sense and I am learning so much for you guys.. I wish there was a way for me to reciprocate the help...
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-10-31 : 11:59:52
you will be able to soon..

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -