现在日期时间减去表格中的日期时间列

时间:2015-03-20 10:07:49

标签: sql sql-server

我有下表

Employee    Designation             Time_In
   A         Manager          07:50:28 19 March 2015
   B         Cashier          07:55:28 19 March 2015
   C         Runner           08:09:38 19 March 2015
   D         Cashier          07:30:32 19 March 2015
   E         Asst Manager     09:11:46 19 March 2015

我如何计算每个人的参与时间。 在表格中,考虑Time Now Time_In - Now()

Employee    Designation            Time_In             Duration      Day_In 
   A         Manager         07:50:28 19 March 2015    02:09:42   19 March 2015
   B         Cashier         07:55:28 19 March 2015    02:04:42   19 March 2015
   C         Runner          08:09:38 19 March 2015    01:50:32   19 March 2015
   D         Cashier         07:30:32 19 March 2015    02:29:38   19 March 2015
   E         Asst Manager    09:11:46 19 March 2015    00:48:14   19 March 2015

2 个答案:

答案 0 :(得分:3)

您只需对SQL Server使用DATEDIFF函数即可计算两个日期之间的差异。它可以返回不同字词的差异,例如secondsminuteshours等。

以下示例说明两个日期之间的小时差异。

DATEDIFF(hour, Time_In, GETDATE())

答案 1 :(得分:1)

SELECT Employee, Designation, Time_In, 
DATEDIFF(hour, Time_In, GETDATE()) + ':' + 
DATEDIFF(minute, Time_In, GETDATE()) + ':' + 
DATEDIFF(second, Time_In, GETDATE()) AS Duration, Day_In
FROM table