?
Solved

Difference of two datetime columns caused overflow at runtime.

Posted on 2005-03-15
3
Medium Priority
?
1,129 Views
Last Modified: 2008-02-01
Got a strange one here experts...

I have a sp to work out the average between 2 datetimes, and then group them by another field...

(See below)

CREATE PROCEDURE [dbo].[Helpdesk_rep16_average_ClosureTime_by_SLA]
/*@start nvarchar(25),
   @end nvarchar(25)*/
AS
SELECT     datediff(minute, Call.LogDatTim, RepSLT.FixDate) AS Diff_in_mins, Call.LogDatTim,
     RepSLT.FixDate, Call.CallNumber, RepSLT.SLT,
     MAX(convert(VARCHAR(10), dateadd(SECOND, SLT_Totals.TotalFixTime / SLT_Totals.CountFixes, '1900-01-01'), 108))
FROM         RepSLT INNER JOIN
                      Call ON RepSLT.CallNumber = Call.CallNumber INNER JOIN
                      (SELECT SLT2.SLT, sum(
                       CASE WHEN Call2.LogDatTim = 0 OR SLT2.FixDate = 0 THEN 0 ELSE
            datediff(SECOND, Call2.LogDatTim, SLT2.FixDate) END) AS TotalFixTime,
                              count(*) AS CountFixes
                       FROM RepSLT SLT2 INNER JOIN
                            Call Call2 ON SLT2.CallNumber = Call2.CallNumber
                       GROUP BY SLT2.SLT) AS SLT_Totals ON SLT_Totals.SLT = RepSLT.SLT
WHERE (NOT (FixDate = CONVERT(DATETIME, '1899-12-30 00:00:00', 102))) and  (NOT (FixDate IS NULL))
GROUP BY datediff(minute, Call.LogDatTim, RepSLT.FixDate), Call.LogDatTim, RepSLT.FixDate, Call.CallNumber, RepSLT.SLT
ORDER BY RepSLT.SLT

However when I try to run it I get Difference of two datetime columns caused overflow at runtime?
On line 10 which is....


SELECT     datediff(minute, Call.LogDatTim, RepSLT.FixDate) AS Diff_in_mins, Call.LogDatTim,


I have tried just running the line on its own and it works fine????

Anyone know how too sort this, please show me by developing it in the above sp...

0
Comment
Question by:paulo111
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 17

Expert Comment

by:BillAn1
ID: 13544672
it's probably the datediff(second....) that is giving you a problem. datediff returns integer, so it will only work for dates that are within 2^31 (max integer) seconds, which works out to be about 68 years.
0
 

Author Comment

by:paulo111
ID: 13545191
No problem, seconds for my averages arent essential... Can someone show me (syntax from the sp above required please) how to do the average for just  hours/mins

Regards
0
 
LVL 34

Accepted Solution

by:
Brian Crowe earned 750 total points
ID: 13546027
CREATE PROCEDURE [dbo].[Helpdesk_rep16_average_ClosureTime_by_SLA]
/*@start nvarchar(25),
   @end nvarchar(25)*/
AS
SELECT DATEDIFF(minute, Call.LogDatTim, RepSLT.FixDate) AS Diff_in_mins,
     Call.LogDatTim,
     RepSLT.FixDate,
     Call.CallNumber,
     RepSLT.SLT,
     MAX(convert(VARCHAR(10), DATEADD(minute, SLT_Totals.TotalFixTime / SLT_Totals.CountFixes, '1900-01-01'), 108))
FROM RepSLT
INNER JOIN Call
     ON RepSLT.CallNumber = Call.CallNumber
INNER JOIN (SELECT SLT2.SLT,
          SUM(CASE
               WHEN Call2.LogDatTim = 0 OR SLT2.FixDate = 0 THEN 0
               ELSE DATEDIFF(minute, Call2.LogDatTim, SLT2.FixDate)
          END) AS TotalFixTime,
          Count(*) AS CountFixes
     FROM RepSLT SLT2
     INNER JOIN Call Call2
          ON SLT2.CallNumber = Call2.CallNumber
     GROUP BY SLT2.SLT) AS SLT_Totals
     ON SLT_Totals.SLT = RepSLT.SLT
WHERE FixDate <> CONVERT(DATETIME, '1899-12-30 00:00:00', 102) and NOT FixDate IS NULL
GROUP BY DATEDIFF(minute, Call.LogDatTim, RepSLT.FixDate), Call.LogDatTim, RepSLT.FixDate, Call.CallNumber, RepSLT.SLT
ORDER BY RepSLT.SLT
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Suggested Courses

765 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