Solved

How to find number of hours between two datetime fields in SQL server

Posted on 2010-11-14
4
405 Views
Last Modified: 2012-05-10
I have two datetimestamp fields  dt1 and dt2
I need to find the difference in hours between dt1 and dt2 in SQL Server

e.g
dt1 = 2010-11-14 13:00:00.000
dt2 = 2010-11-14  15:00:00.000
result is 2 hours

dt1 = 2010-11-13 13:00:00.000
dt2 = 2010-11-14  15:00:00.000
result is 26 hours

dt1 = 2010-11-14 13:00:00.000
dt2 = 2010-11-14  15:30:00.000
result is 2.5 hours
0
Comment
Question by:countrymeister
  • 2
4 Comments
 
LVL 68

Expert Comment

by:Qlemo
ID: 34132350
Depending on the resolution you need, you can do  simple calculation:

select (dt2-dt1)*24
or
select datediff(mi, dt1, dt2)/60.0
or the like.
0
 
LVL 31

Expert Comment

by:James Murrell
ID: 34132357
You want the DATEDIFF function:

SELECT DATEDIFF(hh, @date1, @date2)

if you want minutes as well
select DATEDIFF(hh, @date1, @date2) as Hours_Difference,  
    DATEDIFF(mi,DATEADD(hh,DATEDIFF(hh, @date1, @date2),@date1),@date2) as Minutes_Difference

0
 
LVL 68

Accepted Solution

by:
Qlemo earned 150 total points
ID: 34132362
Sorry, the first one is not correct, we need to convert it to float:

select convert(float, dt2-dt1) * 24

dt2-dt1 results in a datetime representing the difference between both dates. Converting to float it is expressed as fractions of days, so multiplying with 24 (hours per day) makes it fractions of hours.
0
 
LVL 58

Assisted Solution

by:cyberkiwi
cyberkiwi earned 100 total points
ID: 34132366
the first form from qlemo doesn't work in sql server
2nd one does, but depending on precision, you could also use

datediff(ss, dt1, dt2) / 3600.0

or
round((convert(float,d2)-convert(float,d1))*24,5)
0

Featured Post

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how the fundamental information of how to create a table.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now