Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL Datediff function returning minimum value of 1

Posted on 2013-05-28
2
Medium Priority
?
600 Views
Last Modified: 2013-05-28
So I am tracking the average number of days our troublecall tickets are open. Now with the following code i can get close, but my problem that I found is that if a ticket is opened and closed on the same day the Datediff function counts it as 0 days open instead of 1. Is there a better function to use or is there a way to have datediff fulfill the job I need it to do?

Here is the code:

SELECT      DATEPART(iso_week,Closed)as 'ISO Week'
      , AVG(Datediff(DAY,Created,Closed))as 'Avg days to Close'       
      , COUNT(closed)as Closed
FROM TSL
WHERE Priority < 7 and Closed > GETDATE()-273
GROUP BY YEAR(CLOSED), DATEPART(iso_week,Closed)
ORDER BY YEAR(CLOSED), DATEPART(iso_week,Closed)
0
Comment
Question by:NCSA SCADA
2 Comments
 
LVL 22

Accepted Solution

by:
Thomasian earned 2000 total points
ID: 39201891
, AVG(CASE WHEN Datediff(DAY,Created,Closed)=0 THEN 1 ELSE Datediff(DAY,Created,Closed) END)as 'Avg days to Close'
0
 

Author Closing Comment

by:NCSA SCADA
ID: 39202002
Thanks for the quick response. that makes total sense now that i see the code. you saved me alot of time
0

Featured Post

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

Question has a verified solution.

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

Audit has been really one of the more interesting, most useful, yet difficult to maintain topics in the history of SQL Server. In earlier versions of SQL people had very few options for auditing in SQL Server. It typically meant using SQL Trace …
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

877 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