简体   繁体   English

sql计算两次之间的两个日期的分钟数

[英]sql calculating minutes for two dates between two times

I have a table with two DateTimes (StartDateTime, EndDateTime) and I want to calculate the minutes between the two. 我有一个带有两个DateTimes(StartDateTime,EndDateTime)的表,我想计算两个之间的分钟数。

Now, I know to use the DATEDIFF() function to get this, but I only want to get the number of minutes that fall between two TIME(0) values. 现在,我知道要使用DATEDIFF()函数来获取此值,但是我只想获取介于两个TIME(0)值之间的分钟数。

For example: 例如:

  StartDateTime     | EndDateTime         | TimeStart | TimeEnd
2017-06-01 03:00:00 | 2017-06-01 23:00:00 | 06:00:00  | 20:00:00

For the above example, I would get: DATEDIFF(minute, StartDateTime, EndDateTime) = 1200 . 对于上面的示例,我将得到: DATEDIFF(minute, StartDateTime, EndDateTime) = 1200

However, I want to only get the number of minutes that fall between TimeStart and TimeEnd , which would be 840. 但是,我只想获取介于TimeStartTimeEnd之间的分钟数,这将是840。

How can I write my query to get this? 如何编写查询以获取此信息?

In SQL Server, DATEDIFF() works on time values, so you can do: 在SQL Server中, DATEDIFF()可用于时间值,因此您可以执行以下操作:

DATEDIFF(minute, TimeStart, TimeEnd)

You can run this as a demonstration: 您可以将其作为演示运行:

select DATEDIFF(minute, cast('06:00:00' as time), cast('20:00:00' as time))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM