Sometimes things should only happen at certain times. Not long ago I was asked to create a script that is triggered by an event, however, would only execute certain commands if the event occurred during a single specific time range.  Creating an alert to handle the event trigger is the first step and that alert should execute the code below to determine the time window and perform any other processing based on the status of those conditions.

 /* declare script variables */
declare @startTimeWindow time;
 declare @endTimeWindow time;
declare @currentTime time;
/* set user defined parameters */
set @startTimeWindow = '13:00:00';
 set @endTimeWindow = '17:00:00';
/* get the current time */
SELECT @currentTime = CONVERT (time, SYSDATETIME())
/* evaluate the time against the start and end times of the window */
 if((@currentTime > @startTimeWindow) AND (@currentTime < @endTimeWindow))
 /* We are in the time window */
 print 'in window';
/* we are not in the time window */
print 'out of window';
Share This