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
 General SQL Server Forums
 New to SQL Server Programming
 hour and minute

Author  Topic 

peace
Constraint Violating Yak Guru

420 Posts

Posted - 2012-10-04 : 23:03:54
fromDate-->2011-03-06 13:05:00
toDate-->2011-03-06 20:05:00
how can i count the hour and minute together?
it should be 7 hour 0 minute

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2012-10-05 : 00:56:14
You can use the DATEDIFF function for this.

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

Subscribe to my blog
Go to Top of Page

mani_1234
Starting Member

24 Posts

Posted - 2012-10-05 : 00:57:57
create table #t
( fromdate datetime ,
todate datetime )

insert #t
select '2011-03-06 13:05:00','2011-03-06 20:05:00 ' union all
select '2011-03-06 13:05:00','2011-03-06 20:06:00 '
union all select '2011-03-06 13:05:00','2011-03-07 16:06:00 '
union all select '2011-03-06 09:00:00','2011-03-06 18:15:00 '


select * from #t

select convert(varchar(5),datediff(HOUR ,fromdate,todate)) + ' hour '+
convert(Varchar(5),DATEDIFF (Minute,fromdate,todate)%60) +' Minute ' +
convert(Varchar(5),DATEDIFF (SECOND ,fromdate,todate)%60) +' Second '
from #t
Go to Top of Page
   

- Advertisement -