计算HH:MM:SS的时差

时间:2018-01-18 06:27:04

标签: sql-server

我试图获得2次之间的时差。我有一个查询计算以小时为单位的时间,但我想要的是以(hh:mm)(hh:mm:ss)格式计算。 我有什么功能或方法可以达到这个目的吗?

 DECLARE @T TABLE 
    ([EmpID] int, [TimeIn] datetime, [TimeOut] datetime);

INSERT INTO @T
    ([EmpID], [TimeIn], [TimeOut]) VALUES
    (1, '2018-01-10 9:00:00', NULL),
    (1, NULL, '2018-01-10 11:00'),
    (1, '2018-01-10 11:30:00', NULL),
    (1, NULL, '2018-01-10 13:00'),
    (1, '2018-01-10 13:30:00', NULL),
    (1, NULL, '2018-01-10 18:00'),
    (1, '2018-01-11 9:00:00', NULL),
    (1, NULL, '2018-01-11 11:00'),
    (1, '2018-01-11 11:30:00', NULL),
    (1, NULL, '2018-01-11 13:00'),
    (1, '2018-01-11 13:30:00', NULL),
    (1, NULL, '2018-01-11 18:33')
;



SELECT
FirstIN    = CAST(MIN([AttendanceTimeIn]) AS TIME), 
LastOUT    = CAST(MAX([AttendanceTimeOut]) AS TIME), 
HoursSpent = DATEDIFF(HOUR, CAST(MIN(AttendanceTimeIn) AS TIME), CAST(MAX(AttendanceTimeOut) AS TIME)),
CAST(COALESCE(AttendanceTimeIn, AttendanceTimeOut) AS DATE) [Date]

FROM @T

GROUP BY
CAST(COALESCE(AttendanceTimeIn, AttendanceTimeOut) AS DATE)

2 个答案:

答案 0 :(得分:1)

检查此片段,以秒级别返回差异(HH:mm:ss)

SELECT CONVERT(time, 
  DATEADD(s, 
    DATEDIFF(s, 
      '2018-01-07 09:53:00', 
      '2018-01-07 11:53:01'), 
     CAST('1900-01-01 00:00:00.0000000' as datetime2)
   )
)

如果您需要HH:mm,则仅更改's'

'mi'

如果你需要天数差异,也可以使用这个:

SELECT CONVERT(datetime2, 
  DATEADD(s, 
    DATEDIFF(s, 
      '2018-01-07 09:53:00', 
      '2018-01-07 11:53:01'), 
     CAST('0001-01-01 00:00:00.0000000' as datetime2)
   )
)

答案 1 :(得分:1)

您可以将此用于“DATETIME”

DECLARE @START_DATE datetime DECLARE @END_DATE datetime 
--  Set the timeframe variables 
SET @START_DATE = '2011-01-01 16:00:00' SET @END_DATE = '2011-01-01 22:47:22' 
--  Use datediff to show the results in the 
--  HH:MM:SS format 
SELECT CONVERT(VARCHAR(6), datediff(second, @START_DATE, @END_DATE)/3600) + ':' 
+ RIGHT('0' + CONVERT(VARCHAR(2), (datediff(second, @START_DATE, @END_DATE) % 
3600) / 60), 2) + ':' + RIGHT('0' + CONVERT(VARCHAR(2), datediff(second, 
@START_DATE, @END_DATE) % 60), 2) 

结果:

HH:MM:SS
6:47:22

或与TIME

    --  Declare some variables for the requested timeframe 
DECLARE @START_DATE TIME 
DECLARE @END_DATE TIME--  Set the timeframe variables 

SET @START_DATE = '16:00:00' 
SET @END_DATE = '22:47:22'--  Use datediff to show the results in the 

--  HH:MM:SS format 
SELECT CONVERT(VARCHAR(6), Datediff(second, @START_DATE, @END_DATE)/3600) 
       + ':' 
       + RIGHT('0' + CONVERT(VARCHAR(2), (Datediff(second, @START_DATE, 
       @END_DATE) % 
       3600) / 60), 2) 
       + ':' 
       + RIGHT('0' + CONVERT(VARCHAR(2), Datediff(second, @START_DATE, @END_DATE 
       ) % 60) 
       , 2) AS 'HH:MM:SS'

AS Per Edit

; 
WITH cte 
     AS (SELECT EMPID, 
                Min(TIMEIN)                             timein, 
                Max(TIMEOUT)                            timeout, 
                Cast(COALESCE(TIMEIN, TIMEOUT) AS DATE) d 
         FROM   @T 
         GROUP  BY EMPID, 
                   Cast(COALESCE(TIMEIN, TIMEOUT) AS DATE)) 
SELECT EMPID, 
       D AS Day, 
       CONVERT(VARCHAR(6), Datediff(second, TIMEIN, TIMEOUT)/3600) 
       + ':' 
       + RIGHT('0' + CONVERT(VARCHAR(2), (Datediff(second, TIMEIN, TIMEOUT) % 
       3600) / 
       60), 2) 
       + ':' 
       + RIGHT('0' + CONVERT(VARCHAR(2), Datediff(second, TIMEIN, TIMEOUT) % 60) 
       , 2 ) 
         AS [Hours Worked] 
FROM   cte 
ORDER  BY D ASC 

结果

empid   Day            Hours Worked
1       2018-01-10     9:00:00
1       2018-01-11     9:33:00

在preiviuos senario的错误后解决

SELECT FirstIN, LastOUT,

CONVERT(VARCHAR(6), Datediff(second, FirstIN, LastOUT)/3600) 
       + ':' 
       + RIGHT('0' + CONVERT(VARCHAR(2), (Datediff(second, FirstIN, LastOUT) % 
       3600) / 
       60), 2) 
       + ':' 
       + RIGHT('0' + CONVERT(VARCHAR(2), Datediff(second, FirstIN, LastOUT) % 60) 
       , 2 ) AS 

       HoursSpent,[Date] FROM (

SELECT
FirstIN    = CAST(MIN([TimeIn]) AS TIME), 
LastOUT    = CAST(MAX([TimeOut]) AS TIME), 
HoursSpent = DATEDIFF(HOUR, CAST(MIN(TimeIn) AS TIME), CAST(MAX(TimeOut) AS TIME)),
CAST(COALESCE(TimeIn, TimeOut) AS DATE) [Date]

FROM @T

GROUP BY
CAST(COALESCE(TimeIn, TimeOut) AS DATE)) AS a