Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 303
  • Last Modified:

Advanced Datediff?

I have two fields, columns A and B
Both are datetime

A contains date / time a call is logged. B contains the time the call was resolved.

For SLA checking, I need to know how many hours difference there is between them, quite simple so far.

The problem is, the calculation needs to take into account the working day. i.e. 9-5 for example.

So, if a call is logged at 4pm then there is only 1 hour of the SLA time used for the current day and the clock so to speak carrys on again from 9am the next day. Meaning the engineer has until 11am the following day. Thats what I need to show.

Anyone able to help?
0
andy_booth
Asked:
andy_booth
  • 2
  • 2
1 Solution
 
mcmonapCommented:
Hi andy_booth,

How about something along the lines below.  This just checks whether the call was open for more than one day, if that is the case then the number of hours is calculated less 16 (number of hours between 5pm and 9am) multiplied by the number of days.

--start code
DECLARE @tblDates TABLE (logged datetime, resolved  datetime)
INSERT INTO @tblDates VALUES (getdate(), DATEADD(mi,95,getdate()))
INSERT INTO @tblDates VALUES (getdate(), DATEADD(mi,1395,getdate()))
INSERT INTO @tblDates VALUES (getdate(), DATEADD(mi,2795,getdate()))
INSERT INTO @tblDates VALUES (getdate(), DATEADD(mi,2995,getdate()))

SELECT
      logged
      , resolved
      , CASE
            WHEN DATEDIFF(dd, logged, resolved) > 0 THEN DATEDIFF(hh, logged, resolved) - (DATEDIFF(dd, logged, resolved) * 16)
            ELSE DATEDIFF(hh, logged, resolved)
      END WorkingHoursToResolve
FROM
      @tblDates
--end code
0
 
andy_boothAuthor Commented:
Thanks mcmonap

That looks like it would kind of work. Except, what would happen on a Friday, Saturday and Sunday are non working days.

Sorry, maybe I should have said that, assumptions are evil :)

Also, could it be adpated slightly to work on two columns?
For those that have a resolved date, for it to work as is i.e. "Resolved in", for those that the resolved date is empty, for it to work out time remaining.

I will increase the points due to my scope creep.
0
 
LowfatspreadCommented:
so you've got the working shift problem,
and the public holiday problem

i'm surprised that you haven't got the "Call on Hold" situation and
multiple people working on call calculations as well ...

presumably a call can be raised and resolved out of the chargeable hours as well....


0
 
andy_boothAuthor Commented:
For the time being, I would be happy with it working with just 9-5 Mon - Fri.

0
 
mcmonapCommented:
There are probably better ways to do this, but it seems to work:

DECLARE @tblDates TABLE (logged datetime, resolved  datetime)
INSERT INTO @tblDates VALUES (getdate(), DATEADD(mi,95,getdate()))
INSERT INTO @tblDates VALUES (getdate(), DATEADD(mi,1395,getdate()))
INSERT INTO @tblDates VALUES (getdate(), DATEADD(mi,2795,getdate()))
INSERT INTO @tblDates VALUES (getdate(), DATEADD(mi,2995,getdate()))
INSERT INTO @tblDates VALUES (getdate(), DATEADD(dd,5,getdate()))
INSERT INTO @tblDates VALUES (getdate(), DATEADD(dd,9,getdate()))
INSERT INTO @tblDates VALUES (getdate(), DATEADD(mi,19995,getdate()))

SELECT
      logged
      , resolved
      , CASE
            WHEN DATEDIFF(dd, logged, resolved) > 6 OR DATEPART(dw, resolved) < DATEPART(dw, logged) THEN
                        CASE
                              WHEN DATEPART(dw, logged) < DATEPART(dw, resolved) THEN ((DATEDIFF(hh, logged, resolved) - (DATEDIFF(dd, logged, resolved) * 16)) - 16)
                              ELSE (DATEDIFF(hh, logged, resolved) - (DATEDIFF(dd, logged, resolved) * 16)) - (DATEDIFF(wk, logged, resolved) * 16)
                        END
            WHEN DATEDIFF(dd, logged, resolved) > 0 THEN DATEDIFF(hh, logged, resolved) - (DATEDIFF(dd, logged, resolved) * 16)
            ELSE DATEDIFF(hh, logged, resolved)
      END WorkingHoursToResolve
      , DATEPART(dw, resolved) - DATEPART(dw, logged) --<0 =48
      , DATEDIFF(wk, logged, resolved) DiffInWeeks --0
FROM
      @tblDates
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.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now