Solved

MS SQL - Help with DATEDIFF

Posted on 2016-07-21
8
47 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
 
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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
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:ScottPletcher
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:ScottPletcher
ScottPletcher earned 250 total points
ID: 41727719
Sorry:

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

Featured Post

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

760 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now