Solved

Extract exact time and categorize

Posted on 2016-09-12
2
41 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:
ScottPletcher earned 500 total points
Comment Utility
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
Comment Utility
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

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

772 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now