Solved

check current date

Posted on 2006-06-26
8
358 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
CREATE DATABASE ENCRYPTION KEY 1 44
t-sql complement 8 31
SQL 2012 and SQL 2014 always on 9 25
Numeric sequence in SQL 14 37
When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
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…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

706 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

18 Experts available now in Live!

Get 1:1 Help Now