?
Solved

Extract exact time and categorize

Posted on 2016-09-12
2
Medium Priority
?
53 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 2000 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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Suggested Courses

765 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