Find Hours,Minutes,Seconds between two Datetime using SQL

Topics on common programming languages
Post Reply
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Find Hours,Minutes,Seconds between two Datetime using SQL

Post by Neo » Sat Oct 03, 2009 7:01 am

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
Post Reply

Return to “.Net & Other Programming”