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)
 Date Calc

Author  Topic 

Brittney10
Posting Yak Master

154 Posts

Posted - 2013-01-22 : 10:31:37
I'm trying to determine the date difference between two records. For example:

ID_____TYPE______TIME
1______START____1/22/2013 08:00:00AM
1______END______1/22/2013 08:10:00AM
2______START____1/22/2013 09:00:00AM
2______END______1/22/2013 09:20:00AM

RESULTS
ID____DIFF
1_____00:00:10
2_____00:00:20

Basically the difference between start and end time group by ID.

Thanks for the help in advance!

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-01-22 : 10:47:06
If the time column is datetime type, then:
SELECT
id,
CONVERT( VARCHAR(32),
MAX(CASE WHEN TYPE = 'end' THEN TIME END)-
MIN(CASE WHEN TYPE = 'start' THEN TIME END)
,114)
FROM
YourTable
GROUP BY
id
Go to Top of Page

Brittney10
Posting Yak Master

154 Posts

Posted - 2013-01-22 : 11:52:12
Perfect! Exactly what i was trying to accomplish. Thanks!
Go to Top of Page

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-01-22 : 13:02:34
You are very welcome - glad to be of help.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-01-23 : 01:33:27
quote:
Originally posted by Brittney10

Perfect! Exactly what i was trying to accomplish. Thanks!



can there be a data like below

5______START____1/22/2013 09:00:00PM
5______END______1/23/2013 03:20:00AM

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -