Solved

check current date

Posted on 2006-06-26
8
361 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 143

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:Scott Pletcher
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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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
 
LVL 69

Accepted Solution

by:
Scott Pletcher 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:Scott Pletcher
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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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 different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

839 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