Solved

check current date

Posted on 2006-06-26
8
359 Views
Last Modified: 2008-02-01
I only want to run some jobs on M - F, excluding the weekends.  i also need to exclude holidays.
i'm struggling right now to use my calendar table in the job scheduling, which simply is a calendar table, so to speak, indicative of whether the dates in the table are weekdays, weekends, holidays, etc.  Ultimately, I will use this, but right now I really need to modify the job scheduling post haste.  
So, in the interim, what's the right way to only invoke the proc if it's M, T, W, Th or F ?
i thought i could just preface it with an IF, making it execute conditionally, but, it's not working.  (syntax)
what's the right way to do this?

IF (SELECT datepart(dw, getdate()) BETWEEN 2 AND 6
do this.....
0
Comment
Question by:dbaSQL
8 Comments
 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 50 total points
ID: 16987263

DECLARE @dw INT
SET @dw = datepart(dw, getdate())  
IF (@dw >= 2 and @dw <= 6)
BEGIN
  -- do this
END
0
 
LVL 8

Assisted Solution

by:H
H earned 50 total points
ID: 16987276
I believe it would be much easier to schedule these procs in Sql server Agent's job scheduler then you can pick the specific dates?
0
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 16988010
SET DATEFIRST 7  --just to be sure
IF DATEPART(WEEKDAY, GETDATE()) BETWEEN 2 AND 6
BEGIN
    EXEC ...
    ...other command(s)...
END --IF
0
 
LVL 17

Author Comment

by:dbaSQL
ID: 16989163
well, in retrospect, you are right hollecar -- if, in fact, i only want to run M - F, sql server agent scheduling is quite fine, as it allows me to specifically schedule just that - M thru F and/or Saturday and Sunday.  angel and scott, both your suggestions do just that, too.  so, for the way I phrased myself, you've each answered my question, and I will split the points to each of you.  forgive me for having been a bit weird w/this question.

my problem remains, though, as I still cannot exclude holidays.  basically, i've got a whole bunch of agent jobs which i have to vpn in and disable intermittently on holidays and non-work days such that i'm not doing things like sending out blank reports of 'today's trades' for Christmas.  (you know what i mean)  

as i eluded above, I have a calendar table which has 2 years of dates -- attributes for each record are Year, MonthName, DayName, isHoliday, isWeekDay,  etc.  So all the info is there - i just need to someow utilize this with my job scheduling.  any insight or suggestions at all?  i thought possibly a udf that could be called within my procedures, but as of yet, i'm just not getting it together.  my table definition is below, would you guys have any direction/suggestions for me on this?

date - date in smalldatetime
Weekday  - bit  (is it a weekday?  )
Holiday   - bit  (is it a holiday? i.e., christmas, easter, etc., populated manually)
YR - Year
FY - fiscal year
Q - quarter
M - numerical representaiton of month
D - numerical representaiton of day
DW -numerical representaiton of week day
monthname - January, February, etc.
dayname - Monday, Tuesday, etc.
W - week number in the year that date falls in
HolidayDesc -- Christmas, Thanks Giving, etc.
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 69

Accepted Solution

by:
ScottPletcher earned 50 total points
ID: 16992280
SET DATEFIRST 7  --just to be sure
IF DATEPART(WEEKDAY, GETDATE()) BETWEEN 2 AND 6
--add this code
AND NOT EXISTS (SELECT 1 FROM calendar WHERE date = CONVERT(CHAR(8), GETDATE(), 112) AND holiday = 1)
--end: add
BEGIN
    EXEC ...
    ...other command(s)...
END --IF
0
 
LVL 17

Author Comment

by:dbaSQL
ID: 16992400
oh, oh, very good, scott.  i could do this across the board, yes?  like i've got N number of different agent jobs which simply invoke procedures and do this and that.  couldn't i just preface the procedures w/this?
0
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 16992794
Yes, I would think so :-) .
0
 
LVL 17

Author Comment

by:dbaSQL
ID: 16992806
excellent, scott.  it works beautifully.  
i am splitting the points, as i said last night, and just forgot to do.
each of you, thanks very much.
0

Featured Post

[Webinar] Disaster Recovery and Cloud Management

Learn from Unigma and CloudBerry industry veterans which providers are best for certain use cases and how to lower cloud costs, how to grow your Managed Services practice in IaaS clouds, and how to utilize public cloud for Disaster Recovery

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now