如何在SQL Server中计算两个日期之间的小时差(小数)?

编程入门 行业动态 更新时间:2024-10-17 10:37:34
本文介绍了如何在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 的文档:

msdn.microsoft/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:

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

更多推荐

如何在SQL Server中计算两个日期之间的小时差(小数)?

本文发布于:2023-11-22 01:27:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1615437.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:小数   时差   两个   日期   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!