October 29, 2010

Execute T-SQL Code Only on Certain Days




I always clean up my databases manually on Sundays. But at times, if need be, I clean it up on other days as well. However I never do it on Saturdays, since the traffic is usually the most on that day.

So here’s a simple tip using IF-ELSE to prevent my T-SQL code from running on Saturday’s, even if I run the code manually

IF DATEPART(dw,GETDATE())<> 7  -- 7 is Saturday, 1 is Sunday
BEGIN
-- YOUR T-SQL Code Comes Here
SELECT GETDATE()
RETURN
END
ELSE
BEGIN
PRINT
'*** Sorry This Code will Not Execute on Saturdays'
END

OUTPUT

image

You may also want to read Make a T-SQL Query Sleep for a certain amount of time


Did you like this post?
kick it on DotNetKicks.com
subscribe via rss subscribe via e-mail
print this post follow me on twitter


About The Author

Suprotim Agarwal, ASP.NET Architecture MVP works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He has also written an EBook 51 Recipes using jQuery with ASP.NET Controls.

Follow him on twitter @suprotimagarwal

 
  Feedback:

comments

1 Response to "Execute T-SQL Code Only on Certain Days"
  1. Dattatrey Sindol said...
    December 27, 2010 at 8:23 AM

    Hi There,

    DATEPART() function does not return the same value in all the situations since it depends on the settings of SET DATEFIRST option. Instead use DATENAME() function.

    Refer to the following links for more details:

    1. http://dattatreysindol.blogspot.com/2010/08/query-to-get-week-start-date-month.html
    2. http://msdn.microsoft.com/en-us/library/ms174420.aspx

    Thanks,
    Dattatrey Sindol (Datta)
    http://dattatreysindol.blogspot.com

 

Copyright © 2009-2014 All Rights Reserved for SQLServerCurry.com by Suprotim Agarwal | Terms and Conditions