Time Difference w.r.t Dates
-
Actually i just want to output the difference of two Timespans with respect to its dates e.g. 07/03/2009 05:00:00 07/04/2009 10:00:00 and the difference i get is 05:00:00 which is not correct i am using the following code:
DECLARE @EnterTime datetime, @DischargeTime datetime Select @EnterTime = convert(varchar,EnterTime,8) from visit Select @DischargeTime = convert(varchar,NextTime,8) from visit Select convert(varchar,@DischargeTime - @EnterTime,8)
the above code is working fine if the duration is between 24 hours. Can you please suggest me a solution or i have to apply the logic on programmatic side i.e. .NET?AliAmjad(MCP) First make it Run THEN make it Run Fast!
-
Actually i just want to output the difference of two Timespans with respect to its dates e.g. 07/03/2009 05:00:00 07/04/2009 10:00:00 and the difference i get is 05:00:00 which is not correct i am using the following code:
DECLARE @EnterTime datetime, @DischargeTime datetime Select @EnterTime = convert(varchar,EnterTime,8) from visit Select @DischargeTime = convert(varchar,NextTime,8) from visit Select convert(varchar,@DischargeTime - @EnterTime,8)
the above code is working fine if the duration is between 24 hours. Can you please suggest me a solution or i have to apply the logic on programmatic side i.e. .NET?AliAmjad(MCP) First make it Run THEN make it Run Fast!
-
Do you want to get days or hours or minutes between two dates?
I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post.
-
I want to get time in HH:mm:ss format e.g. 13:15:02
AliAmjad(MCP) First make it Run THEN make it Run Fast!
This will do your trick.
DECLARE @d1 DATETIME, @d2 DATETIME, @sd INT
SET @d1 = '20090706 00:00:00'
SET @d2 = '20090707 01:30:01'SET @sd = DATEDIFF(SECOND, @d1, @d2)
SELECT
CASE WHEN @sd/3600<10 THEN '0' ELSE '' END- RTRIM(@sd/3600)
- ':' + RIGHT('0'+RTRIM((@sd % 3600) / 60),2)
- ':' + RIGHT('0'+RTRIM((@sd % 3600) % 60),2) AS Hours
I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post.
-
This will do your trick.
DECLARE @d1 DATETIME, @d2 DATETIME, @sd INT
SET @d1 = '20090706 00:00:00'
SET @d2 = '20090707 01:30:01'SET @sd = DATEDIFF(SECOND, @d1, @d2)
SELECT
CASE WHEN @sd/3600<10 THEN '0' ELSE '' END- RTRIM(@sd/3600)
- ':' + RIGHT('0'+RTRIM((@sd % 3600) / 60),2)
- ':' + RIGHT('0'+RTRIM((@sd % 3600) % 60),2) AS Hours
I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post.