Get Time Duration in [DD hh:mm:ss] format

Waits Stats Duration in [dd hh:mm:ss]
Waits Stats Duration in [dd hh:mm:ss]
Spread the love

As DBA, there are various scenarios where I display duration in results. More often, for visual effects, I like to convert the same duration into [DD hh:mm:ss] format.

For example, for representing the duration of total waits, resource waits & signal waits for wait types from [sys].[dm_os_wait_stats].[wait_time_ms] on Grafana dashboard by converting to [DD hh:mm:ss] format:-

Also, for displaying duration of query execution by converting [sys].[dm_exec_request].[start_time] into [DD hh:mm:ss] format :-

For this purpose, I wrote below TSQL function [time2duration] that provides me duration in [DD hh:mm:ss] format:-

Below are 2 sample usage for above function:-

I hope this will be useful to you 🙂

5 Comments

  1. Dal

    How about this approach

    DECLARE @date1 DATETIME, @date2 DATETIME, @date3 DATETIME

    SET @date1 = ’09 August 2022 09:00:00′
    SET @date2 = ’10 December 2022 23:00:01′

    SELECT @date3 = @date2 – @date1

    SELECT CONVERT (VARCHAR(3), DATEDIFF(DAY,’1900-01-01′,@date3)) + ‘ days, ‘ + CONVERT(VARCHAR(8),@date3,114)

  2. Dal

    For [sys].[dm_exec_requests] i use the follwing sql on the millisecond columns

    DECLARE @milliseconds_elapsed int
    SET @milliseconds_elapsed = 174520549

    SELECT CONVERT(VARCHAR(3),DATEDIFF(DAY,’1900-01-01′,DATEADD(MILLISECOND ,@milliseconds_elapsed,’1900-01-01′) – ‘1900-01-01’)) + ‘d, [‘ + CONVERT(VARCHAR(8),DATEADD(MILLISECOND ,@milliseconds_elapsed,’1900-01-01’) – ‘1900-01-01’,114) + ‘]’ ElapsedTime

  3. mohamadreza

    thank you dear Ajay Dwivedi

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.