Solved

MS SQL - Help with DATEDIFF

Posted on 2016-07-21
8
62 Views
Last Modified: 2016-07-25
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

0
Comment
Question by:MHenry
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 35

Expert Comment

by:YZlat
ID: 41723606
What exactly broke?
0
 
LVL 7

Author Comment

by:MHenry
ID: 41723637
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.
0
 
LVL 27

Expert Comment

by:Chris Luttrell
ID: 41723891
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).
1
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 27

Accepted Solution

by:
Chris Luttrell earned 250 total points
ID: 41723902
Changed
WHERE date_last_modified > DateADD(mi, -15, Current_TimeStamp)
to
WHERE date_last_modified < DateADD(mi, -15, Current_TimeStamp) -- THIS WAS CHECKING date > 15 minutes ago, i.e. within 15 minutes not over 15 minutes old
Here is the code I tested and it works with my assumptions above.  I noted what was changed inline.
CREATE TABLE sometable(date_last_modified DATETIME, soldItems INT)
INSERT INTO dbo.sometable ( date_last_modified, soldItems )
VALUES  ( DATEADD(SECOND,-((15*60)+1), Current_TimeStamp), -- date_last_modified - datetime !!!! Chnage the +1 to -1 or +0 to test the boundary conditions
          RAND()*1000  -- soldItems - int !!!! It doesn't seem to mater what the value of soldItems really is, it is all based on the date
          )
SELECT *, DateADD(mi, -15, Current_TimeStamp), DATEADD(hh,5,DATEADD(day, DATEDIFF(day, 0, date_last_modified - 1), 0)), DATEADD(hh,19,DATEADD(day, DATEDIFF(day, 0, date_last_modified), 0)) FROM dbo.sometable S
IF DATEPART(DW, GETDATE()) IN (2,3,4,5,6)
IF EXISTS
(SELECT TOP 1 date_last_modified, soldItems FROM sometable
WHERE date_last_modified < DateADD(mi, -15, Current_TimeStamp) -- THIS WAS CHECKING date > 15 minutes ago, i.e. within 15 minutes not over 15 minutes old
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

DROP TABLE dbo.sometable;

Open in new window

1
 
LVL 7

Author Comment

by:MHenry
ID: 41723934
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
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 41724968
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
0
 
LVL 7

Author Comment

by:MHenry
ID: 41726019
ScottPletchr,

When I tried yours, I get:
      
'HOUR' is not a recognized built-in function name.
0
 
LVL 69

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 250 total points
ID: 41727719
Sorry:

DATEPART(HOUR, CURRENT_TIMESTAMP) /*instead of HOUR(...)*/
0

Featured Post

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

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…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
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.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

729 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