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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 How to work out the time difference

Author  Topic 

richardlaw
Yak Posting Veteran

68 Posts

Posted - 2009-01-13 : 14:34:09
Hi

I'm using a simple table with two values (timeFrom and timeTo) each as dateTime. I'd like to create a view that works out just the time difference between the two, for example 1.5 would be 1 and 1/2 hours difference. I'm not at all interested in the date values.

Many thanks
Richard

Richard Law

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-01-13 : 14:48:58
You can use the DATEDIFF function to find the difference between the two values.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

rohitkumar
Constraint Violating Yak Guru

472 Posts

Posted - 2009-01-13 : 14:55:21
quote:
Originally posted by richardlaw

Hi

I'm using a simple table with two values (timeFrom and timeTo) each as dateTime. I'd like to create a view that works out just the time difference between the two, for example 1.5 would be 1 and 1/2 hours difference. I'm not at all interested in the date values.

Many thanks
Richard

Richard Law



as long as your record is of datetime data type, it will have the date component. what if the time difference is 27.5 hours, what would you want to display here?
Go to Top of Page

richardlaw
Yak Posting Veteran

68 Posts

Posted - 2009-01-13 : 14:58:57
The time difference will only ever be between 30 minutes and 2.5 hours. Is there a way to work this out discarding the date info?

Thanks
Richard
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-01-13 : 15:25:22
Just use DATEDIFF as I already mentioned.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

rohitkumar
Constraint Violating Yak Guru

472 Posts

Posted - 2009-01-13 : 15:28:28
DECLARE @DATE1 DATETIME
DECLARE @DATE2 DATETIME

SET @DATE1 = '2009-01-13 15:30:00'
SET @DATE2 = '2009-01-13 13:00:00'

SELECT DATEDIFF(N, @DATE2, @DATE1) MINUTES
Go to Top of Page
   

- Advertisement -