2016-08-15 49 views
1

我有這個疑問日期部分結果格式隨着時間

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的專欄。 lebihInTimeOutTime之間的結果範圍。

我有兩個問題。

  1. 從我上面的查詢我只能得到hour。因此,如何將它與這種格式hh:mm

  2. 如何轉換,看(OutTime - InTime)之間的範圍內,然後用- 08:00減?

+0

什麼是範圍在時間和outime意味着愨你可以分享的價值是如何計算的 – TheGameiswar

+0

2016年8月28日21一些信息:03:00.000 - 2016年8月28日08:55: 00.000但是隻有'datetime部分' – YVS1102

+0

我沒有得到它,哪個日期時間部分,是幾分鐘,..? – TheGameiswar

回答

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' 

UPDATE:

要減去從Hotel詳細信息8個小時,你可以用

CONVERT(VARCHAR(5), DATEADD(HOUR, -8, OutTime - InTime), 108) AS lebih 
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小時,以進一步分鐘的時間,您就可以把OutTime8小時落後第一位。

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

我該怎麼做'lebih coloum' - '08:00'? – YVS1102

+0

@ YVS1102對不起,我不太明白。你的意思是扭轉時間8個小時? – ydoow

+0

我的意思是減去8小時 – YVS1102

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

+0

你能解釋一下這個'2011-01-01 00:00'是什麼? – YVS1102

+0

和我可以在查詢內做數學嗎?例如'lebih - 08:00 hour' – YVS1102

+0

的值,這只是將結果轉換爲預期時間格式的一種技巧..只需將時間和第二個字段添加爲00:00的日期(任何日期) datediff函數。我們不能明確地將微小差異轉換爲時間數據類型。 –

1

對於問題編號1.U可以使用@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