Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在SQL Server中,当开始时间和结束时间超过12点时,如何获取正确的时间格式_Sql_Sql Server_Sql Server 2008 - Fatal编程技术网

在SQL Server中,当开始时间和结束时间超过12点时,如何获取正确的时间格式

在SQL Server中,当开始时间和结束时间超过12点时,如何获取正确的时间格式,sql,sql-server,sql-server-2008,Sql,Sql Server,Sql Server 2008,我尝试过员工的打卡打卡时间,我尝试过下面的查询,当打卡时间为上午10:00,打卡打卡时间为下午6:00时效果良好,显示的总持续时间为8小时 但当打卡时间如9:00pm和5:00am时,它就失败了 关于这个问题,我根据打卡和打卡进行了计算,显示了总小时数 但当用户在凌晨12点后打卡时,打卡和打卡上都显示为00:00,总持续时间显示为0 我得到的输出 I:9:05AM\nO:5:32PM\nT:8:27 \nS :~Present On WeeklyOff - WeekOff,:Saturd

我尝试过员工的打卡打卡时间,我尝试过下面的查询,当打卡时间为上午10:00,打卡打卡时间为下午6:00时效果良好,显示的总持续时间为8小时

但当打卡时间如9:00pm和5:00am时,它就失败了

关于这个问题,我根据打卡和打卡进行了计算,显示了总小时数

但当用户在凌晨12点后打卡时,打卡和打卡上都显示为00:00,总持续时间显示为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'
选择
(按e.EmployeeCode、AttendanceDate DESC排序)上方的行号()为“SNo”,
e、 员工代码为“EmpID”,
e、 员工名称为“EmpName”,
d、 部门名称“DeptName”,
将(VARCHAR,a.AttendanceDate,110)转换为“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,当a.OutTime类似“%”时,然后LEFT(a.OutTime,a.OutTime+'))-1)
ELSE a.OutTime END,0),100),7)+'\n'+'T:'+转换(字符(5),转换(CAST(当a.InTime>a.OutTime然后DATEDIFF(分钟,转换(a.InTime作为datetime),DateAdd(天,1,转换(a.OutTime作为datetime)))
ELSE DATEDIFF(MINUTE,a.InTime,a.OutTime)END AS INTEGER)/60 AS VARCHAR)+':'+CAST(CAST(当a.InTime>a.OutTime然后是DATEDIFF(MINUTE,CAST(a.InTime作为datetime),DateAdd(day,1,CAST(a.OutTime作为datetime))时的情况)
ELSE DATEDIFF(MINUTE,a.InTime,a.OutTime)以整数结尾)%60作为VARCHAR),108)+'\n'+'S:~'+a.[Status]+'-'+当a.[Status]='Present'时为''.[Status]='On WeeklyOff'时为'.[Status]='On WeeklyOff',la.LeaveType'',然后为la.LeaveType时为'
当DATENAME(dw,a.AttendanceDate)=“星期日”时,则当DATENAME(dw,a.AttendanceDate)=“星期六”时,则为“周末”
然后“WeekOff”ELSE la.LeaveType将SQL\u Latin1\u General\u CP1\u CI\u整理为ENd+”,:“+DATENAME(dw,a.AttendanceDate)+”,:“+如果la.LeaveStatus为null,则“ELSE la.LeaveStatus”结束为“PunchDetails”,
“0LOP”作为“NoOfLOP”,
“0WkEnd”作为“NoOfWkEndWorked”,ISNULL(la.LeaveStatus,”)作为“LeaveStatus”
--进入#结果02
从…起
esslsmartofficenew.dbo.e(NOLOCK)
内连接
e.EmployeeId=a.EmployeeId上的esslsmartofficenew.dbo.AttendanceLogs a(NOLOCK)
内连接
esslsmartofficenew.dbo.Departments d(NOLOCK)ON d.DepartmentId=e.DepartmentId
内连接
esslsmartofficenew.dbo.ShiftId上的s(NOLOCK)移位=a.ShiftId
左连接
IntranetReleaseNew.dbo.LeaveApplication la ON la.EmpId collate SQL\u Latin1\u General\u CP1\u CI\u AS=e.employeecode collate SQL\u Latin1\u General\u CP1\u CI\u AS
和a.AttendanceDate>=la.leavefromdate

a.AttendanceDate我不想尝试解开你的代码,所以我只想处理相关的计算

如果我们可以假设,当停机时间小于开始时间时,则停机时间发生在第二天,我们可以使用如下方式:

注意:为了减少重复将时间转换为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演示:

返回:

+------------+----------------+--------+---------+-----------------+---------------+
| 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         |
+------------+----------------+--------+---------+-----------------+---------------+

嗨,即使在周末,它在所有的日子里都可以正常工作,但在一个场景中它失败了。当一个人的时间是下午6点和凌晨4点(例如),它失败了。我的意思是,如果它在周六超过11点59分,它的时间是12点和12点