Solved

DateDiff not factoring in AM/PM?

Posted on 2011-09-26
4
290 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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks again!
0
 
LVL 92

Expert Comment

by:Patrick Matthews
Comment Utility
We all have those days :)
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

Suggested Solutions

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
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.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

728 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

11 Experts available now in Live!

Get 1:1 Help Now