Link to home
Start Free TrialLog in
Avatar of MHenry
MHenry

asked on

MS SQL - Help with DATEDIFF

I am using software that allows me to send alerts using SQL.

The idea is to send an alert if soldItems hasn't been updated in 15 minutes between the hours of 5:00am and 7:00pm, and to not send alerts during weekends and between 7:01pm and 4:59am.

I had it set to send alerts between 7:00pm and 1:00am and it seemed to be working. I changed it to 7:00pm and 5:00am and now it's broken. Not sure what I did wrong ...

date_last_modified is one of the columns in the table. The "Select 0 ELSE Select 1" is done only to give me a score I can base the alert on.

Any ideas? I'm stuck and would appreciate help.

IF DATEPART(DW, GETDATE()) IN (2,3,4,5,6)
IF EXISTS
(SELECT TOP 1 date_last_modified, soldItems FROM table.dbo
WHERE date_last_modified > DateADD(mi, -15, Current_TimeStamp) AND date_last_modified >= DATEADD(hh,5,DATEADD(day, DATEDIFF(day, 0, date_last_modified - 1), 0)) AND date_last_modified <  DATEADD(hh,19,DATEADD(day, DATEDIFF(day, 0, date_last_modified), 0))
ORDER BY date_last_modified DESC)
SELECT 0
ELSE
SELECT 1

Open in new window

Avatar of YZlat
YZlat
Flag of United States of America image

What exactly broke?
Avatar of MHenry
MHenry

ASKER

I'm getting alerts during the time I'm not supposed to be getting them. I still get alerts, but they shouldn't be coming.
Are you wanting a 0 or 1 to indicate to send the Alert?
I take it the rules are
1.  IF the date_last_modified > 15 minutes old, i.e. it is sometime before 15 minutes before right now
2. AND the current hour of the day is between 5 AM and 7 PM
3. THEN send the alert (return 0?, I think based on your sample code)
if either of those conditions are not met, do not send an alert (return 1).
ASKER CERTIFIED SOLUTION
Avatar of Chris Luttrell
Chris Luttrell
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of MHenry

ASKER

Chris,

That may well be the answer. I was thinking "within 15 minutes" but you're right, it should be "over 15 minutes." I was so convinced it was an error in the way I was telling time that I didn't even check that!

I'll give this a shot and let you know as soon as I can. I have to let it run at least overnight though.

Thanks for the help!

mh
I removed DATEPART() to eliminate any dependency on SQL date settings.  The DATEDIFF() calc below works accurately with any/all date/language settings.

IF DATEDIFF(DAY, 0, CURRENT_TIMESTAMP) % 7 <= 4 /*Mon-Fri only*/
AND HOUR(CURRENT_TIMESTAMP) BETWEEN 5 AND 18 /*5AM-6:59:59.997PM only*/
AND NOT EXISTS(SELECT 1 FROM table.dbo WHERE date_last_modified >= DATEADD(MINUTE, -15, CURRENT_TIMESTAMP)) /*no activity within 15 mins only*/
    /*If ALL above conditions are true, send alert*/
    SELECT 1
ELSE
    SELECT 0
Avatar of MHenry

ASKER

ScottPletchr,

When I tried yours, I get:
      
'HOUR' is not a recognized built-in function name.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial