Please start any new threads on our new site at 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
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 hour and minute
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Constraint Violating Yak Guru

420 Posts

Posted - 10/04/2012 :  23:03:54  Show Profile  Reply with Quote
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

Almighty SQL Goddess

38200 Posts

Posted - 10/05/2012 :  00:56:14  Show Profile  Visit tkizer's Homepage  Reply with Quote
You can use the DATEDIFF function for this.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server

Subscribe to my blog
Go to Top of Page

Starting Member

24 Posts

Posted - 10/05/2012 :  00:57:57  Show Profile  Reply with Quote
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
  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.02 seconds. Powered By: Snitz Forums 2000