如何在SQL Server中12 AM之后到达InTime和OutTime时获取正确的时间格式

时间:2017-06-06 14:00:09

标签: sql sql-server sql-server-2008

我已经尝试过冲击员工的打卡时间,我已经尝试了以下查询,当打卡时间如上午10:00并打出时间如下午6:00时,它工作正常,并显示TotalDuration为8小时。

但是当时间如下午9点和早上5点时,它会失败。

关于这个问题,我已经完成了基于打卡和计算的计算,显示总小时数。

但是当用户在12点之后打拳时,它会在打卡和打卡时显示为00:00,而TotalDuration显示为0

我得到的输出

    I:9:05AM\nO:5:32PM\nT:8:27 \nS :~Present On WeeklyOff - WeekOff,:Saturday,:    
    I:12:00AM\nO:12:00AM\nT:0:0  \nS :~Present On WeeklyOff - WeekOff,:Saturday,:

这是我的问题:

SELECT 
    ROW_NUMBER() OVER (ORDER BY e.EmployeeCode,AttendanceDate DESC) AS 'SNo',  
    e.EmployeeCode AS 'EmpID',  
    e.EmployeeName AS 'EmpName',  
    d.DepartmentFName AS 'DeptName', 
    CONVERT(VARCHAR, a.AttendanceDate, 110) AS 'AttendanceDate',
    'I:' + LTRIM (RIGHT(CONVERT(VARCHAR(50),+'I:'+CONVERT(VARCHAR(50),+CONVERT(DATETIME, a.InTime,0)),100),7))+'\n'+'O:'+LTRIM (RIGHT(CONVERT(VARCHAR(20),CONVERT(DATETIME, CASE WHEN a.OutTime LIKE '%(%' THEN LEFT(a.OutTime, CHARINDEX('(', a.OutTime + ')') - 1)  
   ELSE a.OutTime END,0),100),7))+'\n'+ 'T:'+ CONVERT(CHAR(5),CAST(CAST(CASE WHEN  a.InTime> a.OutTime THEN DATEDIFF (MINUTE,CAST(a.InTime as datetime),DateAdd(day,1,CAST(a.OutTime AS datetime)))
 ELSE DATEDIFF(MINUTE, a.InTime , a.OutTime) END AS INTEGER)/60 AS VARCHAR)+':'+CAST(cast(CASE WHEN  a.InTime> a.OutTime THEN DATEDIFF (MINUTE,CAST(a.InTime as datetime),DateAdd(day,1,CAST(a.OutTime AS datetime)))
 ELSE DATEDIFF(MINUTE, a.InTime , a.OutTime) END AS INTEGER)%60 AS VARCHAR),108)+ '\n' + 'S :~' +  a.[Status]+ ' - ' + CASE WHEN a.[Status] = 'Present' THEN '' WHEN a.[Status] = 'On WeeklyOff' and la.LeaveType<>'' THEN la.LeaveType   
   WHEN DATENAME(dw, a.AttendanceDate)='Sunday' THEN 'WeekOff' WHEN DATENAME(dw, a.AttendanceDate)='SaturDay'   
   THEN 'WeekOff' ELSE la.LeaveType COLLATE SQL_Latin1_General_CP1_CI_AS   ENd + ',:'+DATENAME(dw, a.AttendanceDate) + ',:'+ CASE WHEN la.LeaveStatus is null THEN '' ELSE la.LeaveStatus END AS 'PunchDetails',
   '0LOP' AS 'NoOfLOP',
   '0WkEnd' AS 'NoOfWkEndWorked' ,ISNULL(la.LeaveStatus,'') AS 'LeaveStatus' 

-- INTO #Result02  
FROM   
    esslsmartofficenew.dbo.Employees e (NOLOCK) 
INNER JOIN
    esslsmartofficenew.dbo.AttendanceLogs a (NOLOCK) ON e.EmployeeId = a.EmployeeId 
INNER JOIN   
    esslsmartofficenew.dbo.Departments d (NOLOCK) ON d.DepartmentId = e.DepartmentId 
INNER JOIN  
    esslsmartofficenew.dbo.Shifts s (NOLOCK) ON s.ShiftId = a.ShiftId 
LEFT JOIN  
    IntranetReleaseNew.dbo.LeaveApplication la ON la.EmpId collate SQL_Latin1_General_CP1_CI_AS = e.employeecode collate SQL_Latin1_General_CP1_CI_AS 
                                               AND a.AttendanceDate >= la.leavefromdate 
                                               AND a.AttendanceDate <= la.leaveTodate  
WHERE   
    e.DepartmentId = '10' AND  
    e.EmployeeName NOT LIKE '%del_%' AND
    e.EmployeeCode LIKE '%S%' AND 
    e.EmployeeCode NOT LIKE '%Del_%' AND
    a.AttendanceDate = '2017-05-13'

1 个答案:

答案 0 :(得分:0)

我不想尝试解开你的代码,所以我只想解决相关的计算问题。

如果我们可以假设当停机时间小于银行时间,则第二天发生的停机时间我们可以使用这样的东西:

注意:我使用common table expression来减少重复使用上述假设将时间转换为datetime的代码,您可以使用子查询/派生表来达到相同的效果。

create table t (employeeid int, attendancedate date, intime time(0), outtime time(0));
insert into t values (1,'20170601','21:00:00','05:00:00'),(2,'20170603','05:00:00','21:00:00');

;with cte as (
select *
  , indatetime =dateadd(minute,datediff(minute,0,intime),convert(datetime2(0),attendancedate))
  , outdatetime=case when outtime > intime
      then dateadd(minute,datediff(minute,0,outtime),convert(datetime2(0),attendancedate))
      else dateadd(minute,datediff(minute,0,outtime),convert(datetime2(0),dateadd(day,1,attendancedate)))
      end
from t
)
select 
    employeeid
  , attendancedate
  , intime = ltrim(right(convert(varchar(23),convert(datetime,intime ),100),7))
  , outtime= ltrim(right(convert(varchar(23),convert(datetime,outtime),100),7))
  /* if less than 24 hours we can use this simpler version: */
  , TotalDuration24 = convert(time(0),dateadd(minute,datediff(minute,indatetime,outdatetime),0))
  /* if values greater than 24 hours we would use this version: */
  , TotalDuration = convert(varchar(10),
      convert(varchar(4),(datediff(minute,indatetime,outdatetime)/60),2)+':'
     +right('00'+convert(varchar(2),datediff(minute,indatetime,outdatetime)%60),2)
    )
from cte

rextester演示:http://rextester.com/OQFS42186

返回:

+------------+----------------+--------+---------+-----------------+---------------+
| employeeid | attendancedate | intime | outtime | TotalDuration24 | TotalDuration |
+------------+----------------+--------+---------+-----------------+---------------+
|          1 | 2017-06-01     | 9:00PM | 5:00AM  | 08:00:00        | 8:00          |
|          2 | 2017-06-03     | 5:00AM | 9:00PM  | 16:00:00        | 16:00         |
+------------+----------------+--------+---------+-----------------+---------------+