calculating duration between two datetime stamps - SQL Server

Viewed 43

is it possible to get an exact duration between timestamps? for example;

Start time = 2022-02-24 11:27:00
End time = 2022-04-26 12:00:00
Duration = 48hrs 33mins

Originally I was using the date diff but that only brings back the hours but the requirements have since changed. Also tried using date name & date part functions..

any suggestions??

1 Answers

I tend to use the Dateadd DateDiff pattern

DECLARE @then DATETIME ='2022-02-24 11:27:00'
DECLARE @now DATETIME ='2022-02-26 12:00:00'
DECLARE @diff DATETIME = DATEADD(MINUTE,DATEDIFF(MINUTE,@then,@now),0)
    
SELECT CAST(DATEDIFF(hh,0,@diff) as VARCHAR(10))+'hrs '+
CAST(DATEPART(MINUTE,@diff)AS VARCHAR(2)) +'mins' 
Related