简体   繁体   English

SQL DateDiff在几分钟内跨越整个夜晚

[英]SQL DateDiff in Minutes Spanning Over Night

Trying to get the difference in minutes between a datetime field and a time field. 尝试获取日期时间字段和时间字段之间以分钟为单位的差异。 I'm using the datediff function. 我正在使用datediff函数。

When the start time starts on one date like '2018-01-08 22:35:55.043' and the end time is the following day like '00:35:56.2136644', the result is counting from the end time to the start time. 当开始时间开始于某个日期(例如“ 2018-01-08 22:35:55.043”)并且结束时间是第二天(例如“ 00:35:56.2136644”)时,结果从结束时间开始计算。

Examples: 例子:

select DATEDIFF(MINUTE, CAST('2018-01-08 22:35:55.043' AS TIME), '00:35:56.2136644') AS minDiff1
select DATEDIFF(MINUTE,  '00:35:56.2136644', CAST('2018-01-08 22:35:55.043' AS TIME)) AS minDiff2
select DATEDIFF(MINUTE, CONVERT(TIME, '2018-01-08 22:35:55.043'), '00:35:56.2136644') AS minDiff3
select DATEDIFF(MINUTE, '00:35:56.2136644', CONVERT(TIME, '2018-01-08 22:35:55.043')) AS minDiff4

The results were different from what I was expecting. 结果与我预期的不同。 The desired result would be 120 minutes. 期望的结果将是120分钟。

minDiff1 = -1320

minDiff2 = 1320

minDiff3 = -1320

minDiff4 = 1320

Original Query 原始查询

select DATEDIFF(MINUTE, CAST(test_start_datetime as TIME), test_end_time) AS minDiff
from user_exam  

The following assumes that the time belongs to the same day or the very next day: 以下假设时间属于同一天或第二天:

SELECT *, CASE
    -- same day -- start time is less than end time
    WHEN CAST(datetimecol AS time) <= timecol THEN DATEDIFF(MINUTE, CAST(datetimecol AS time), timecol)
    -- next day -- start time is more than end time (it rolled over into next day)
    ELSE 1440 - DATEDIFF(MINUTE, timecol, CAST(datetimecol AS time))
END
FROM (VALUES
    (CAST('2018-01-08 22:35:55.043' AS DATETIME), CAST('22:35:55.0433333' AS TIME)),
    (CAST('2018-01-08 22:35:55.043' AS DATETIME), CAST('23:35:56.2136644' AS TIME)),
    (CAST('2018-01-08 22:35:55.043' AS DATETIME), CAST('00:35:56.2136644' AS TIME))
) AS tests(datetimecol, timecol)

In the above example 1440 is the number of minutes in 24 hours. 在上面的示例中,1440是24小时内的分钟数。

Demo on DB Fiddle DB Fiddle上的演示

Let us first reconsider your examples: 让我们首先重新考虑您的示例:

select DATEDIFF(MINUTE, CAST('2018-01-08 22:35:55.043' AS TIME), '00:35:56.2136644') AS minDiff1
select DATEDIFF(MINUTE,  '00:35:56.2136644', CAST('2018-01-08 22:35:55.043' AS TIME)) AS minDiff2
select DATEDIFF(MINUTE, CONVERT(TIME, '2018-01-08 22:35:55.043'), '00:35:56.2136644') AS minDiff3
select DATEDIFF(MINUTE, '00:35:56.2136644', CONVERT(TIME, '2018-01-08 22:35:55.043')) AS minDiff4

Converting/casting from datetime to time throws the date part away. 从日期时间转换/广播到时间将日期部分丢掉。 Thus, you are actually running: 因此,您实际上正在运行:

select DATEDIFF(MINUTE, '22:35:55.043'', '00:35:56.2136644') AS minDiff1
select DATEDIFF(MINUTE,  '00:35:56.2136644', '22:35:55.043') AS minDiff2
select DATEDIFF(MINUTE, '22:35:55.043', '00:35:56.2136644') AS minDiff3
select DATEDIFF(MINUTE, '00:35:56.2136644'22:35:55.043') AS minDiff4

At this point, you seem to not be considering the fact that DATEDIFF is directed, meaning it counts (first argument units passed) FROM second argument TO third argument. 在这一点上,您似乎没有考虑DATEDIFF是有方向的,这意味着它从第二个参数到第三个参数都计数(传递的第一个参数单位)。 Thus, since 00:35 is 1320 minutes earlier than 22:35 (of the same day), 00:35 -> 22:35 returns 1320, whereas 22:35 -> 00:35 returns -1320. 因此,由于00:35比当天的22:35早1320分钟,因此00:35-> 22:35返回1320,而22:35-> 00:35返回-1320。


To be completely precise, since DATEDIFF uses datetimes, your times/time-representing strings are implicitly converted to dates. 确切地说,由于DATEDIFF使用日期时间,因此您的时间/时间表示字符串会隐式转换为日期。 Since a date is not provided by you, the date used is the one with value 0: 1st January 1900. That's the common day the function acts upon. 由于您没有提供日期,因此使用的日期是1900年1月1日的值0:这是函数执行的常见日期。

If you want to see 120 that's mean time difference is not enough for your purpose. 如果您想看到120,则意味着时差不足以实现您的目的。 With your question you want to find difference between 22:35 and following day 00:35 You need to find DATETIME difference something like that: 对于您的问题,您想找到22:35和第二天00:35之间的时差。您需要找到DATETIME时差,例如:

SELECT DATEDIFF(MINUTE,         CAST(GETDATE() AS DATETIME)
                               +CAST(CAST('22:35:55.2136644' AS TIME) AS DATETIME)
                             , 
                                CAST(GETDATE() AS DATETIME)+1
                               +CAST(CAST('00:35:56.2136644' AS TIME)AS DATETIME)

                ) AS minDiff1

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

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