Solved

MS SQL - Help with DATEDIFF

Posted on 2016-07-21
8
52 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 26

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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
LVL 26

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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MS SQL + Insert Into Table - If Doesnt Exist 9 36
Passing Parameter to Stored Procedure 4 24
Sql Query 6 68
Help to build a Proc... 6 23
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
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
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to shrink a transaction log file down to a reasonable size.

831 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