Solved

DateDiff not factoring in AM/PM?

Posted on 2011-09-26
4
299 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
[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
  • 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

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.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

734 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