Datediff() 重叠的日子?

Datediff() overlapping days?

我无法弄清楚如何对重叠的日子使用 datediff() 函数。

这是我的 table 和我遇到问题的黄色标记行。

SELECT 
    FOMTID,
    TOMTID,
    DATEDIFF(MINUTE, FOMTID,TOMTID) TIME 
FROM [DATABASE]...

没有有像'2001-01-01 23:30'和'2001-01-02 00:00'这样的字段这将使日期差异功能有效,但我只有时间。

如果这些是时间数据类型:

SELECT FOMTID, TOMTID,
       (CASE WHEN FOMTID <= TOMTID THEN DATEDIFF(MINUTE, FOMTID, TOMTID)
             ELSE 24*60 - DATEDIFF(MINUTE, TOMTID, FOMTID)
        END) AS TIME 

Here有点SQL Fiddle.

LiveDemo

SELECT 
  FOMTID,
  TOMTID, 
  [TIME] = IIF(FOMTID <= TOMTID,
              DATEDIFF(MINUTE, FOMTID,TOMTID),
              1440 - DATEDIFF(MINUTE, TOMTID,FOMTID))
FROM your_table;

或:

SELECT 
  FOMTID,
  TOMTID, 
  [TIME] = IIF(FOMTID <= TOMTID,
                     DATEDIFF(MINUTE, FOMTID,TOMTID),
              1440 + DATEDIFF(MINUTE, FOMTID,TOMTID))
FROM your_table;

编辑:

LiveDemo2

WITH cte AS
(
SELECT 
  FOMTID,
  TOMTID, 
  [TIME] = IIF(FOMTID <= TOMTID,
                     DATEDIFF(MINUTE, FOMTID,TOMTID),
              1440 + DATEDIFF(MINUTE, FOMTID,TOMTID))
FROM #tab
)
SELECT FOMTID, TOMTID
   ,[MINUTES] = [TIME]
   ,[TIME]  = CAST([TIME] / 60  + [TIME] % 60 / 100.0 AS DECIMAL(8,2))
   ,[TIME2] = CAST([TIME] / 60  + [TIME] % 60 / 60.0 AS DECIMAL(8,2))
FROM cte

选择您要如何处理会议记录部分:

[Time]  => 30min => 0.3
[Time2] => 30min => 0.5