Solved

Extract exact time and categorize

Posted on 2016-09-12
2
47 Views
Last Modified: 2016-09-12
I have a field that is formatted like "9/9/2016 8:00AM".  I would like to use this field to categorize whether an entry is "On Time" or "Late".  So far I am able to say that any thing after the hour of 5AM is late, however this categorizes the the 5:00AM time as "Late".  We also have two shifts/day so my code contains a nested IF to look at both 5am and 2pm.  

Ideally it would count if anything between 5:01AM and 12:00PM and between 2:01PM and 11:00PM as "Late" in the "Status" column.  Here is my code so far:

SELECT EmpNumber, FirstName, LastName, ClockIn, ClockOut, IIf((DatePart(hour,ClockIn) Between 5 And 12) Or (DatePart(hour,clockin) Between 14 And 21),'Late','On Time') AS Status

FROM v_MES_TcActivities

GROUP BY EmpNumber, FirstName, LastName, ClockIn, ClockOut, IIf((DatePart(hour,clockin) Between 5 And 12) Or (DatePart(hour,clockin) Between 14 And 21),'Late','On Time'), CostCtr

HAVING (((ClockIn) Between Getdate()-30 And Getdate()) AND ((IIf((DatePart(hour,clockin) Between 5 And 12) Or (DatePart(hour,clockin) Between 14 And 21),'Late','On Time')) Like 'Late') AND ((CostCtr)='106330'))

ORDER BY LastName, ClockIn DESC

Open in new window

0
Comment
Question by:Michael Larimore
2 Comments
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 500 total points
ID: 41794559
I don't see the need for GROUP BY in this query, so I may not fully understand what you need yet.  But the hour checking is fairly straight forward:


SELECT EmpNumber, FirstName, LastName, ClockIn, ClockOut,
    CASE WHEN ((DATEPART(HOUR, ClockIn) = 5 AND DATEPART(MINUTE, ClockIn) > 0) OR DATEPART(HOUR, ClockIn) BETWEEN 6 AND 11)
           OR ((DATEPART(HOUR, ClockIn) = 14 AND DATEPART(MINUTE, ClockIn) > 0) OR DATEPART(HOUR, ClockIn) BETWEEN 15 AND 22)
         THEN 'Late' ELSE 'On Time' END AS Status

FROM v_MES_TcActivities

WHERE (((ClockIn) Between Getdate()-30 And Getdate()) AND ((CostCtr)='106330'))

ORDER BY LastName, ClockIn DESC
0
 

Author Closing Comment

by:Michael Larimore
ID: 41794690
Thank you that is perfect!!  I built the code using access query builder which usually adds in the default "group by" function for some reason.
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question