Solved

MS SQL - Help with DATEDIFF

Posted on 2016-07-21
8
53 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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Suggested Solutions

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

856 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