上午的SQL DATEDIFF

时间:2015-08-27 09:54:20

标签: sql sql-server time datediff

我想计算两次之间的时差,但我的问题可能是第二次是在午夜之后,第一次是在它之前:

time 1  = '2015-08-02 09:30 PM'   
time 2  = '2015-08-03 02:30 AM'

所以在这种情况下,他们之间的差异将是负面的, 我尝试将 -1 返回的值乘以将其转换为正值,但其他数据将是错误的。

我的代码是:

DECLARE @StartDate datetime = '2015-08-02';
DECLARE @EndDate datetime = '2015-08-02';
DECLARE @EmpID nvarchar(6) = '12345';

SELECT
SUM(CASE WHEN(DATEDIFF(minute, a.[Time], @StartDate)) < 0
then DATEDIFF(minute, a.[Time], @StartDate) * -1 
ELSE DATEDIFF(minute, a.[Time], @StartDate) END
)

FROM
Attendance a
WHERE
(EmpID = @EmpID OR @EmpID IS NULL)
AND a.[date] <= @EndDate
GROUP BY EmpID

那我怎么解决呢?

0 个答案:

没有答案