Time Diffrence in SQL
-
Hi all, I have a table like :- Start Time End Time ---------- --------- 12/12/2008 1:46:26 PM 12/12/2008 11:48:06 PM how can i get the diffrence between these two dates? (it should be in HH:MM:SS format.....) Thanks in Advance...
Start by looking at the DATEDIFF function.
-
Hi all, I have a table like :- Start Time End Time ---------- --------- 12/12/2008 1:46:26 PM 12/12/2008 11:48:06 PM how can i get the diffrence between these two dates? (it should be in HH:MM:SS format.....) Thanks in Advance...
Here you go.. pretty darn simples.
declare @dt1 as datetime
set @dt1 = '2008-12-12 13:46:26'
declare @dt2 as datetime
set @dt2 = '2008-12-12 23:48:06'declare @dt3 datetime
set @dt3 = @dt2-@dt1
select CONVERT(VARCHAR(10),@dt3,108)108 is the format for HH:mi:ss
-
Hi all, I have a table like :- Start Time End Time ---------- --------- 12/12/2008 1:46:26 PM 12/12/2008 11:48:06 PM how can i get the diffrence between these two dates? (it should be in HH:MM:SS format.....) Thanks in Advance...
Mr. J4amieC solution is perfect which will work everywhere. However, if you are using Sql Server 2008, you can use the new TIME datatype something like this declare @tbl table(startdate datetime,enddate datetime) insert into @tbl select '2008-12-12 13:46:26','2008-12-12 23:48:06'
select CAST((select enddate-startdate from @tbl) as time) TimeDiff
:)
Niladri Biswas
modified on Wednesday, October 28, 2009 5:56 AM
-
Mr. J4amieC solution is perfect which will work everywhere. However, if you are using Sql Server 2008, you can use the new TIME datatype something like this declare @tbl table(startdate datetime,enddate datetime) insert into @tbl select '2008-12-12 13:46:26','2008-12-12 23:48:06'
select CAST((select enddate-startdate from @tbl) as time) TimeDiff
:)
Niladri Biswas
modified on Wednesday, October 28, 2009 5:56 AM
Let me get this straight, you are casting a DATETIME to a DATETIME so you can cast it to TIME? :wtf: :omg:
only two letters away from being an asset
-
Let me get this straight, you are casting a DATETIME to a DATETIME so you can cast it to TIME? :wtf: :omg:
only two letters away from being an asset
Yae.. I didnot notice that I made that mistake. Very sorry. But thanks. :)
Niladri Biswas