?
Solved

Extract exact time and categorize

Posted on 2016-09-12
2
Medium Priority
?
58 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 70

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

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

Question has a verified solution.

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

When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
In a use case, a user needs to close an opened report by simply pressing the Escape (Esc) key. This can be done by adding macro code in Report_KeyPress or Report_KeyDown event.
Viewers will learn how the fundamental information of how to create a table.
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

862 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