Solved

Extract exact time and categorize

Posted on 2016-09-12
2
49 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 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
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…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

730 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