Solved

DateDiff not factoring in AM/PM?

Posted on 2011-09-26
4
294 Views
Last Modified: 2012-06-27
I've attached some code and the data results.  What I think is happening is the code is not factoring in the am/pm.  Why else would it show 9 hrs as the dif between 12:00 am and 9:00 am?  What do I need to change to fix this?

SELECT     a.Name, a.EmployeeId, a.Expected, a.Actual, n.ShiftNote, u.UserName, CONVERT(varchar, FLOOR(a.Tardy / 60.0)) + ':' + RIGHT('0' + CONVERT(varchar, a.Tardy % 60), 
                      2) AS HrMin
FROM         (SELECT     CONVERT(varchar(20), s.TimeIn, 100) AS Expected, s.TimeIn, l.LastName, l.FirstName, l.EmployeeId, l.ManagerUID, 
                                              l.LastName + ', ' + l.FirstName AS Name, CONVERT(varchar(20), MIN(h.TimeIn), 100) AS Actual, CASE WHEN s.TimeIn < MIN(h.TimeIn) 
                                              THEN DATEDIFF(MINUTE, s.TimeIn, ISNULL(MIN(h.TimeIn), s.TimeIn)) END AS Tardy, MIN(h.RecordId) AS RecordId
                       FROM          dbo.EmployeeHours AS h INNER JOIN
                                              dbo.EmployeeSchedules AS s ON h.EmployeeId = s.EmployeeId AND DATEADD(day, 0, DATEDIFF(DAY, 0, s.TimeIn)) = DATEADD(DAY, 0, DATEDIFF(DAY, 0, 
                                              h.TimeIn)) INNER JOIN
                                              dbo.EmployeeList AS l ON s.Company = l.Company AND s.EmployeeId = l.EmployeeId
                       WHERE      (l.Suspend = 0) AND (DATEADD(DAY, 0, DATEDIFF(DAY, 0, h.TimeIn)) BETWEEN @From AND @To)
                       GROUP BY l.EmployeeId, l.ManagerUID, s.TimeIn, l.LastName, l.FirstName) AS a LEFT OUTER JOIN
                      dbo.UserList AS u ON a.ManagerUID = u.UID LEFT OUTER JOIN
                      dbo.EmployeeShiftNotes AS n ON a.RecordId = n.RecordId AND a.EmployeeId = n.EmployeeId
WHERE     (a.Tardy >= 0) AND (u.UserName = @Supervisor)
ORDER BY a.LastName, a.FirstName, a.TimeIn

Results...
Emp Id	Name	Expected	           Actual	       Hrs:Min
3227	Doe, Jane	Sep  2 2011 11:30AM	Sep  2 2011 11:31AM	0:01
		Sep  5 2011 12:00AM	Sep  5 2011  9:00AM	9:00
		Sep  8 2011  9:00AM	Sep  8 2011  9:01AM	0:01

Open in new window

0
Comment
Question by:BobRosas
  • 2
  • 2
4 Comments
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 125 total points
ID: 36600834
BobRosas,

>>Why else would it show 9 hrs as the dif between 12:00 am and 9:00 am?

Why do you think that's the wrong answer?

I mean, if you go from 12:00 AM to 9:00 AM, that is 9 hours.

:)

Patrick
0
 

Author Comment

by:BobRosas
ID: 36600848
You are right.  I was reading the data and knew what it should say (12:00pm)  but didn't read what it actually said.  Sorry to bother you!
0
 

Author Closing Comment

by:BobRosas
ID: 36600852
Thanks again!
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 36600881
We all have those days :)
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

832 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