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
You can can see that value is completely incorrect
3条答案
按热度按时间9gm1akwq1#
it calculate
datediff()
inminute
because usingsecond
ormillisecond
will surely cause overflow when the date is far apart.dbfiddle
You also need to increase the size of the return string
RETURNS VARCHAR(10)
asdd:hh:mm:ss.mmm
is 15 charactersEDIT : to handle where
@date_start
is later than@date_en
Also added a
sg
to indicate when such case, the sign is-
zbdgwd5y2#
The function returns
varchar(10)
. Thedd:hh:mm:ss:ms
pattern is 14 characters.xcitsw883#
If you just output your two columns to SSRS, a TimeSpan can be used to get the difference between two DateTime values to great precision, but then... how to format it? Apparently SSRS can't handle TimeSpans directly in an expression, but the Code option can. Therefore:
Also noteworthy... the format strings for TimeSpan are different from those for (.NET)DateTime:
Custom TimeSpan Format Strings