两个日期之间的DATEDIFF返回时间戳

时间:2020-05-28 12:52:47

标签: sql sql-server tsql datediff

已解决

CONCAT((DATEDIFF(Minute,START_DTTM,END_DTTM)/60),'h:',
       (DATEDIFF(Minute,START_DTTM,END_DTTM)%60) 'm') AS TotalTimeMissing 

产生TotalTimeMissing:5h:13m

________

我试图返回两个特定日期之间的时间戳记值,以计算出包裹丢失和被发现之间的时间。

编辑:代码已更新为包括来自萨米语的代码。我还添加了我从原始代码中排除的其他代码。

这是当前代码:

USE PACKAGE

GO  

SELECT

        dp.LEGACY_ID

       ,dp.SURNAME

       ,dp.FORENAME

       ,dp.ETHNICITY_DESCRIPTION

       ,dp.BIRTH_DTTM

       ,DATEDIFF(YY, dp.BIRTH_DTTM, GETDATE()) -

            CASE

                  WHEN RIGHT(CONVERT(VARCHAR(6), GETDATE(), 12), 4) >=

                        RIGHT(CONVERT(VARCHAR(6), dp.BIRTH_DTTM, 12), 4)

                        THEN 0

                  ELSE 1

            END AS [Current Age]

--^Precise age calc due to potential LL inaccuracy

       ,mp.DIM_PERSON_ID

       ,mp.MISSING_STATUS

       ,mp.START_DTTM

       ,mp.END_DTTM

       ,dp.LEGACY_ID

       ,mp.RETURN_INT_OFFERED

       ,mp.RETURN_INT_ACCEPTED

       ,mp.RETURN_INT_DATE


FROM C_S.FACT_MISSING_PACKAGE AS mp

JOIN C_S.FACT_MISSING_PACKAGE AS dp ON mp.DIM_PERSON_ID = dp.DIM_PERSON_ID

WHERE CAST (mp.START_DTTM AS DATE)

              BETWEEN DATEADD(YY, -1, CAST (GETDATE() AS DATE)) AND CAST (GETDATE() AS DATE)

--^Displays all records within exactly 1 year of run date

       UNION (SELECT CONCAT(Value / 3600 / 24,

              ' Days ',

              RIGHT(CONCAT('00', Value / 3600 % 24), 2),

              ':',

              RIGHT(CONCAT('00', Value / 60 % 60), 2),

              ':',

              RIGHT(CONCAT('00',Value % 3600 % 60), 2)

       ) AS TotalTimeMissing

FROM

(

  SELECT mp.DIM_PERSON_ID, DATEDIFF(Second, mp.START_DTTM, mp.END_DTTM) Value

  FROM C_S.FACT_MISSING_PACKAGE AS mp

) T(Value))


ORDER BY START_DTTM ASC;

Sami帮了我很大的忙,但是当我运行上述代码时,我遇到了与UNION和T有关的错误,T没有说明所需的列数。为了解决这个问题,我尝试将SELECT列的第一轮放入(SELECT CONCAT()语句中,但是它会产生错误,因此在解决方法时我有些茫然?

我需要返回所有这些列,并在末尾添加一个额外的列,作为TotalTimeMissing

谢谢

3 个答案:

答案 0 :(得分:1)

您可以先使用DATEDIFF函数以分钟为单位计算差异,然后在知道1小时为60分钟而1天为1440分钟的情况下计算小时和天数。

请注意DATEDIFF 在SQL Server中有效:

此函数返回整数的计数(作为有符号整数值) 指定的日期部分边界在指定的开始日期之间交叉 和结束日期。

所以

DATEDIFF(day, '2020-01-13 23:59:58', '2020-01-14 00:00:08')
即使相差仅几秒钟,

也将返回1,因为给定的时间间隔越过了一天的边界(午夜)。

这就是为什么您不应该在这里使用DATEDIFF(day, ...),而是使用DATEDIFF(minute, ...)DATEDIFF(second, ...)并根据经过的分钟或秒的总数来计算小时和天数的原因。< / p>

我将使用CROSS APPLY避免多次键入长表达式。 我还在这里使用整数除法/,它舍弃了小数部分,例如200 / 60 = 3

Total days = total minutes / 1440 (discard fractional part)
Total hours = total minutes / 60 (discard fractional part)

但是,我们并不需要总计小时,而是需要一整天之后剩余的小时数,因此我们需要采用mod 24。

Hours = Total hours % 24

对于最后几分钟,我们只需要在总天数和小时数之后的其余分钟,因此

Minutes = total minutes mod 60.

查询:

SELECT 
     dp.LEGACY_ID
    ,dp.SURNAME
    ,dp.FORENAME
    ,mp.DIM_PERSON_ID
    ,mp.MISSING_STATUS
    ,mp.START_DTTM
    ,mp.END_DTTM
    ,dp.LEGACY_ID
    ,STR(DiffMinutes / 1440) + ':' +        -- total days
     STR(DiffMinutes / 60 % 24) + ':' +     -- hours (0 .. 23)
     STR(DiffMinutes % 60) AS TimeMissing   -- minutes (0 .. 59)
FROM 
    MissingPackages AS mp
    JOIN DIM_PERSON AS dp ON mp.DIM_PERSON_ID = dp.DIM_PERSON_ID
    CROSS APPLY
    (
        SELECT DATEDIFF(minute, mp.START_DTTM, mp.END_DTTM) AS DiffMinutes
    ) AS A
ORDER BY START_DTTM ASC;

答案 1 :(得分:0)

CONCAT(DATEDIFF(day, START_DT, END_DT), '-', DATEDIFF(hour, START_DT, END_DT), '-', DATEDIFF(minute, START_DT, END_DT)) AS TimeMissing

答案 2 :(得分:0)

这是要找的

CREATE TABLE MyData 
(
  StartDate DATETIME, 
  EndDate DATETIME
);

INSERT INTO MyData VALUES
('2017-01-01 00:00:00', '2018-01-02 00:25:01'),
('2017-01-01 00:00:00', '2018-01-01 00:00:00'),
('2017-01-02 12:00:09', '2017-01-02 12:00:30'),
('2017-01-01 02:00:00', '2017-01-01 03:30:30'),
('2017-01-01 00:00:00', '2017-01-03 00:30:30'),
('2017-12-31 23:59:59', '2018-01-01 00:00:01'),
('2017-12-31 23:59:01', '2018-01-01 00:00:01');


SELECT CONCAT(Value / 3600 / 24, 
              ' Days ', 
              RIGHT(CONCAT('00', Value / 3600 % 24), 2), 
              ':', 
              RIGHT(CONCAT('00', Value / 60 % 60), 2), 
              ':',
              RIGHT(CONCAT('00',Value % 3600 % 60), 2)
       ) TimeMissing 
FROM
(
  SELECT DATEDIFF(Second, StartDate, EndDate) Value
  FROM MyData
) T(Value);

返回:

+-------------------+
|    TimeMissing    |
+-------------------+
| 366 Days 00:25:01 |
| 365 Days 00:00:00 |
| 0 Days 00:00:21   |
| 0 Days 01:30:30   |
| 2 Days 00:30:30   |
| 0 Days 00:00:02   |
| 0 Days 00:01:00   |
+-------------------+