Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 DateDiff Function

Author  Topic 

Sanjay_Bakshi
Starting Member

14 Posts

Posted - 2008-07-16 : 17:33:42
Dear All,

I have two dates Date1 and Date2

The diffrence of these two dates should come in HH:MM:SS

Example
Date1 - 07/14/2008 01:01:01
Date2 - 07/14/2008 02:02:02

Answer should be 01:01:01

Please help me with query

Sanjay

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-07-16 : 17:39:46
[code]DECLARE @Date1 DATETIME,
@Date2 DATETIME

SELECT @Date1 = '07/14/2008 01:01:01',
@Date2 = '07/14/2008 02:02:02'

SELECT CONVERT(CHAR(8), DATEADD(SECOND, ABS(DATEDIFF(SECOND, @Date1, @Date2)), '00:00:00'), 108)[/code]


E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2008-07-16 : 18:32:57
This also works:
declare	@date1 datetime,
@date2 datetime

select @date1 = '07/14/2008 01:01:01',
@date2 = '07/14/2008 02:02:02'

select TimeDiff= convert(char(8),@date2-@date1,108)


Results:
TimeDiff
--------
01:01:01

(1 row(s) affected)


CODO ERGO SUM
Go to Top of Page
   

- Advertisement -