我有这个查询
fa-5x
从查询中我得到了这个结果
SELECT
d.Nip,d.FullName,b.*,c.InTime,c.OutTime,
(SELECT DATEPART(HOUR, OutTime)) - (SELECT DATEPART(HOUR, InTime)) as lebih
FROM
DinasAuditHeader a
INNER JOIN
DinasAuditDetail b on a.KodeAuditHeader = b.KodeAuditHeader
INNER JOIN
Attendance c on b.Nip = c.Nip and b.attendancedate = c.AttendanceDate
INNER JOIN
employee d on c.Nip = d.Nip
WHERE
b.Nip = '1502427'
AND c.AttendanceCode = 'P3'
AND a.KodeAuditHeader = 'AD0000001'
从我的查询和结果中可以看出。我有一个名为Nip FullName KodeDetailDinas KodeAuditHeader nip AttendanceDate claim_status InTime OutTime lebih
1502427 FERNANDO ALIM 1 AD0000001 1502427 2016-08-28 0 2016-08-28 08:55:00.000 2016-08-28 21:03:00.000 13
1502427 FERNANDO ALIM 3 AD0000001 1502427 2016-08-30 0 2016-08-30 08:55:00.000 2016-08-30 18:03:00.000 10
1502427 FERNANDO ALIM 2 AD0000001 1502427 2016-08-29 0 2016-08-29 08:55:00.000 2016-08-29 19:03:00.000 11
1502427 FERNANDO ALIM 4 AD0000001 1502427 2016-08-31 0 2016-08-31 08:50:00.000 2016-08-31 20:03:00.000 12
的列。 lebih
是lebih
和InTime
之间的结果范围。
我有两个问题。
从我上面的查询中我只能获得OutTime
。那么如何使用这种格式hour
如何查看它们之间的范围(hh:mm
- OutTime
),然后用InTime
减去?
答案 0 :(得分:1)
您可以尝试SELECT CONVERT(VARCHAR(5),(OutTime - InTime),108) AS lebih
下面的Sql查询应该适用于您的情况:
SELECT
d.Nip,d.FullName,b.*,c.InTime,c.OutTime,
CONVERT(VARCHAR(5),(OutTime - InTime),108) AS lebih
FROM
DinasAuditHeader a
INNER JOIN
DinasAuditDetail b on a.KodeAuditHeader = b.KodeAuditHeader
INNER JOIN
Attendance c on b.Nip = c.Nip and b.attendancedate = c.AttendanceDate
INNER JOIN
employee d on c.Nip = d.Nip
WHERE
b.Nip = '1502427'
AND c.AttendanceCode = 'P3'
AND a.KodeAuditHeader = 'AD0000001'
<强>更新强>
要从lebih中减去8小时,您可以使用
CONVERT(VARCHAR(5), DATEADD(HOUR, -8, OutTime - InTime), 108) AS lebih
答案 1 :(得分:1)
问题编号1.你可以使用@Unnikrishnan R回答。然后对于第二个,请尝试下面的查询。
SELECT
d.Nip,d.FullName,b.*,c.InTime,c.OutTime, LEFT(CAST(DATEADD(MINUTE,DATEDIFF(MINUTE,InTime,OutTime),'2011-01-01 00:00') AS TIME),5) lebih ,
LEFT(CAST(DATEADD(MINUTE,DATEDIFF(MINUTE,InTime,DATEADD(HOUR, -8, OutTime)),'2011-01-01 00:00') AS TIME),5)
FROM DinasAuditHeader a
INNER JOIN DinasAuditDetail b on a.KodeAuditHeader = b.KodeAuditHeader
INNER JOIN Attendance c on b.Nip = c.Nip and b.attendancedate = c.AttendanceDate
INNER JOIN employee d on c.Nip = d.Nip
WHERE b.Nip = '1502427' and c.AttendanceCode = 'P3' and a.KodeAuditHeader = 'AD0000001'
order by KodeDetailDinas asc
答案 2 :(得分:0)
VARCHAR
后附加到':00'
的简单投射怎么样?
SELECT
d.Nip,d.FullName,b.*,c.InTime,c.OutTime,
cast((SELECT DATEPART(HOUR, OutTime)) - (SELECT DATEPART(HOUR, InTime)) AS VARCHAR(2)) + ':00' as lebih
FROM DinasAuditHeader a
inner join DinasAuditDetail b on a.KodeAuditHeader = b.KodeAuditHeader
inner join Attendance c on b.Nip = c.Nip and b.attendancedate = c.AttendanceDate
inner join employee d on c.Nip = d.Nip
where b.Nip = '1502427' and c.AttendanceCode = 'P3' and a.KodeAuditHeader = 'AD0000001'
如果您想使其精确到MINUTE
,请更好地使用DATEDIFF
select
d.Nip,d.FullName,b.*,c.InTime,c.OutTime,
cast(DATEDIFF(MINUTE, InTime, OutTime) / 60) as varchar(2)) + ':'
+ cast(DATEDIFF(MINUTE, InTime, OutTime) % 60) as varchar(2))
FROM DinasAuditHeader a
inner join DinasAuditDetail b on a.KodeAuditHeader = b.KodeAuditHeader
inner join Attendance c on b.Nip = c.Nip and b.attendancedate = c.AttendanceDate
inner join employee d on c.Nip = d.Nip
where b.Nip = '1502427' and c.AttendanceCode = 'P3' and a.KodeAuditHeader = 'AD0000001'
如果您希望将时间结果进一步缩短8小时,则可以先将OutTime
8小时后退。
select
d.Nip,d.FullName,b.*,c.InTime,c.OutTime,
cast(DATEDIFF(MINUTE, InTime, DATEADD(HOUR, -8, OutTime)) / 60) as varchar(2)) + ':'
+ cast(DATEDIFF(MINUTE, InTime, DATEADD(HOUR, -8, OutTime)) % 60) as varchar(2))
FROM DinasAuditHeader a
inner join DinasAuditDetail b on a.KodeAuditHeader = b.KodeAuditHeader
inner join Attendance c on b.Nip = c.Nip and b.attendancedate = c.AttendanceDate
inner join employee d on c.Nip = d.Nip
where b.Nip = '1502427' and c.AttendanceCode = 'P3' and a.KodeAuditHeader = 'AD0000001'
答案 3 :(得分:0)
使用脚本获取'HH:MM'的时差。
SELECT
d.Nip,d.FullName,b.*,c.InTime,c.OutTime,
LEFT(CAST(DATEADD(MINUTE,DATEDIFF(MINUTE,InTime,OutTime),'2011-01-01 00:00') AS TIME),5) as lebih
FROM DinasAuditHeader a
INNER JOIN DinasAuditDetail b on a.KodeAuditHeader = b.KodeAuditHeader
INNER JOIN Attendance c on b.Nip = c.Nip and b.attendancedate = c.AttendanceDate
INNER JOIN employee d on c.Nip = d.Nip
WHERE b.Nip = '1502427' and c.AttendanceCode = 'P3' and a.KodeAuditHeader = 'AD0000001'
查看示例查询的结果。 Time difference in HH:MM