UpwardD
UpwardD

Reputation: 767

Convert The Difference Between StartDate and EndDate Into Hours, Minutes And Seconds

Suppose I have a StartDate and an EndDate, how do I format or convert the difference into hh:mm:ss? Thank you.

Upvotes: 1

Views: 269

Answers (1)

Vignesh Kumar A
Vignesh Kumar A

Reputation: 28403

Try this

SELECT CONVERT(Varchar(10), Dateadd(Second, DATEDIFF(Second,StartDate,EndDate),0), 108)

For Example:

DECLARE @StartDate DateTime
DECLARE @EndDate DateTime

SET @StartDate = '2014-06-08 07:18:12.893'
SET @EndDate = '2014-06-09 05:58:40.893'

SELECT CONVERT(Varchar(10), Dateadd(Second, DATEDIFF(Second,@StartDate,@EndDate),0), 108)

Output:

+---------+
|  Time   |
+---------+
|22:40:28 |
+---------+

Fiddle Demo

Upvotes: 1

Related Questions