如何在SQL Server中计算两个日期之间的小时差(小数)? [英] How to calculate difference in hours (decimal) between two dates in SQL Server?

查看:2368
本文介绍了如何在SQL Server中计算两个日期之间的小时差(小数)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须计算SQL Server 2008中两个日期之间的小时差(小数类型)。



我找不到任何有用的技术来将datetime转换为十进制与MSDN上的CONVERT。

有人可以帮助我吗?



更新:

要清楚,我需要小数部分好(因此十进制类型)。所以从9点到10点半应该回到我1.5。

解决方案

DATEDIFF(小时, start_date,end_date)将给出 start_date end_date 之间的小时边界数



如果您需要小数小时数,可以使用 DATEDIFF 以更高的分辨率划分结果:

  DATEDIFF(第二,start_date,end_date)/ 3600.0 

MSDN上提供 DATEDIFF 的文档:



http://msdn.microsoft.com/en- us / library / ms189794%28SQL.105%29.aspx


I have to calculate the difference in hours (decimal type) between two dates in SQL Server 2008.

I couldn't find any useful technique to convert datetime to decimal with 'CONVERT' on MSDN.
Can anybody help me with that?

UPDATE:
To be clear, I need the fractional part as well (thus decimal type). So from 9:00 to 10:30 it should return me 1.5.

解决方案

DATEDIFF(hour, start_date, end_date) will give you the number of hour boundaries crossed between start_date and end_date.

If you need the number of fractional hours, you can use DATEDIFF at a higher resolution and divide the result:

DATEDIFF(second, start_date, end_date) / 3600.0

The documentation for DATEDIFF is available on MSDN:

http://msdn.microsoft.com/en-us/library/ms189794%28SQL.105%29.aspx

这篇关于如何在SQL Server中计算两个日期之间的小时差(小数)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆