Page 1 of 1

Find Hours,Minutes,Seconds between two Datetime using SQL

Posted: Sat Oct 03, 2009 7:01 am
by Neo
In this short snippet, we find the Hours, Minutes and Seconds in between two Datetime.

-- Find Hours, Minutes and Seconds in between two datetime

Define dummy start and stop Date/Time:

Code: Select all

DECLARE @First datetime
DECLARE @Second datetime
SET @First = '04/02/2008 05:23:22'
SET @Second = getdate()
Code to get difference as Hours Minutes and Seconds:

Code: Select all

SELECT DATEDIFF(day,@First,@Second)*24 as TotalHours,
DATEDIFF(day,@First,@Second)*24*60 as TotalMinutes,
DATEDIFF(day,@First,@Second)*24*60*60 as TotalSeconds