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

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Date Calc
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Brittney10
Posting Yak Master

USA
154 Posts

Posted - 01/22/2013 :  10:31:37  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

3873 Posts

Posted - 01/22/2013 :  10:47:06  Show Profile  Reply with Quote
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

USA
154 Posts

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

James K
Flowing Fount of Yak Knowledge

3873 Posts

Posted - 01/22/2013 :  13:02:34  Show Profile  Reply with Quote
You are very welcome - glad to be of help.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52326 Posts

Posted - 01/23/2013 :  01:33:27  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000