I am not a developer so please pardon my question. I am more of prod DBA
We have an SSRS page which displays the long running SQL queries and show elapsed time. But we needed a way to show duration like dd:hh:mm:ss:ms
Therefore i got the function from link here with code as below
--get the difference between two datetimes in the format: 'hh:mm:ss'
CREATE FUNCTION getDateDiff(@startDate DATETIME, @endDate DATETIME)
RETURNS VARCHAR(10)
AS BEGIN
DECLARE @seconds INT = DATEDIFF(s, @startDate, @endDate)
DECLARE @difference VARCHAR(10) =
FORMAT(@seconds / 3600, '00') + ':' +
FORMAT(@seconds % 3600 / 60, '00') + ':' +
FORMAT(@seconds % 60, '00')
RETURN @difference
END
But cant get this to work if i need day and milliseconds as well.
How can i use above to display dd:hh:mm:ss:ms or return data in this format? And we have to make sure it works for SQL2012 and +
IF there is a better code out there please guide me as i am novice to SQL dev part, thanks
Edit- Getting -ve value in seconds as mentioned in comment
With the 2nd edit i am seeing issues with day now:
Please see below
for start time "5/21/2021 8:00:23 PM" (ET) and end date "5/22/2021 01:09:0 6 AM"
it returned me 1 5:9:17.13 which is incorrect as should not be 1 day and 5 hours rather should be 5 hours so something is going wrong on day