?
Solved

MS SQL - Help with DATEDIFF

Posted on 2016-07-21
8
Medium Priority
?
107 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
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 27

Accepted Solution

by:
Chris Luttrell earned 1000 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 70

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 70

Assisted Solution

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

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

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
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.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Suggested Courses

749 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