2017-03-18 112 views
1

我想知道哪個用戶是晚上9點鐘的時鐘在晚上9點00分上午9點多宣佈爲晚,但我的結果顯示我超過了10點00分只是算晚了在SQL中轉換日期的時間

DECLARE @clockin as varchar 
DECLARE @clockout as varchar 
DECLARE @reportdate as datetime 

--SET @clockin = CONVERT(108,'08:30') 
SET @clockin = CONVERT(varchar(10),CAST('9:00' AS TIME),100) 
SET @clockout = CONVERT(varchar(10),CAST('18:30' AS TIME),100) 
SET @reportdate = month(GETDATE()) 

SELECT 
    u.showname AS showname, 
    l.USERID AS USERID, 
    u.BADGENUMBER AS BADGENUMBER, 
    l.CHECKTIME AS CHECKTIME, 
    CASE 
     WHEN DATEPART(HOUR, l.CHECKTIME) <= @clockin 
      THEN CONVERT(varchar(10), CAST(l.CHECKTIME AS TIME), 100) 
      ELSE 'late ' + CONVERT(varchar(100), CAST(l.CHECKTIME AS TIME), 100) 
    END AS Time 
FROM 
    CHECKINOUT l 
INNER JOIN 
    USERINFO u ON l.USERID = u.USERID 
WHERE 
    u.showname IS NOT NULL 
    AND u.BADGENUMBER > 100 
    AND CHECKTIME >= '1 jan 2017' 
    AND CHECKTIME <= '31 jan 2017' 
--GROUP BY l.USERID, u.showname, u.BADGENUMBER 
ORDER BY 
    u.BADGENUMBER 

結果

enter image description here

+0

哪個RDBMS是爲了這個?請添加一個標籤來指定您是使用'mysql','postgresql','sql-server','oracle'還是'db2' - 或者其他的東西。 –

回答

0

比較時間不只是'小時'!

DECLARE @clockin as TIME 
DECLARE @clockout as TIME 
DECLARE @reportdate as datetime 

SET @clockin = CAST('9:00' AS TIME) 
SET @clockout = CAST('18:30' AS TIME) 
SET @reportdate = month(GETDATE()) 

SELECT 
    u.showname, l.USERID, u.BADGENUMBER, l.CHECKTIME, 
    CASE 
     WHEN CAST(l.CHECKTIME AS TIME) < = @clockin 
      THEN CONVERT(VARCHAR(10), CAST(l.CHECKTIME AS TIME), 100) 
      ELSE 'late ' + CONVERT(VARCHAR(100), CAST(l.CHECKTIME AS TIME), 100) 
    END AS Time 
FROM 
    CHECKINOUT l 
INNER JOIN 
    USERINFO u ON l.USERID = u.USERID 
WHERE 
    u.showname IS NOT NULL 
    AND u.BADGENUMBER > 100 
    AND CHECKTIME >= '1 jan 2017' 
    AND CHECKTIME <= '31 jan 2017' 
ORDER BY 
    u.BADGENUMBER 
0

所有你需要做的就是刪除了等號。該人抵達九點,所以他們不遲到,因爲這等於開始時間。您也可以在辦理入住手續時提前59秒退房,因此上午9點30分不會遲到。 我會建議使用一個單獨的領域較晚標誌,使人們可以更方便地在Excel或任何使用在其他報告中這樣的數據:如果人是晚多少,他們是遲到的東西一樣

Select 
    u.showname, l.USERID, u.BADGENUMBER, l.CHECKTIME, 
    case 
     when datepart(HOUR,l.CHECKTIME) < @clockin