We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Advanced Datediff?

andy_booth
andy_booth asked
on
Medium Priority
317 Views
Last Modified: 2008-02-01
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?
Comment
Watch Question

CERTIFIED EXPERT

Commented:
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

Author

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.
CERTIFIED EXPERT
Top Expert 2011

Commented:
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....


Author

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

CERTIFIED EXPERT
Commented:
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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.