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 2008 Forums
 Transact-SQL (2008)
 date difference

Author  Topic 

joe8079
Posting Yak Master

127 Posts

Posted - 2011-11-25 : 13:16:04
Hello,

I have two columns and i'm trying to calculate the difference between the two time frames in hours:minutes:seconds, but its not coming out correctly:

column 1 column 2
18:24:00 - 17:00:00
I would like the new column to say 1 HR 24 Minute Difference

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2011-11-25 : 13:27:33
col2-col1 will give the time diference

convert(varchar(20),datepart(hh,col2-col1)) + ' hours ' + convert(varchar(20),datepart(mi,col2-col1)) + 'minute difference'

Do you really want to do this formatting in sql though.


==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

joe8079
Posting Yak Master

127 Posts

Posted - 2011-11-25 : 13:54:16
awesome, thanks that works great
Go to Top of Page

vikki.seth
Yak Posting Veteran

66 Posts

Posted - 2011-11-25 : 14:41:16
you also try with datediff() function.
http://msdn.microsoft.com/en-us/library/ms189794.aspx
Go to Top of Page

joe8079
Posting Yak Master

127 Posts

Posted - 2011-11-25 : 16:00:29
Thanks, i'll give that a shot as well.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-11-26 : 12:19:35
quote:
Originally posted by joe8079

Thanks, i'll give that a shot as well.


see this too

http://visakhm.blogspot.com/2010/03/time-difference-function.html

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

Go to Top of Page
   

- Advertisement -