I have a query that calculates the hours worked from dates which includes times.
e.g 1/07/2011 7:00:00 AM – 1/07/2011 5:40:00 PM – 0.5 = 10.166666
The SQL is
SELECT entityID, StaffID, ActualDate,
DATEDIFF(minute, StartTime, EndTime) / 60.0 lunch AS HoursWorked
FROM dbo.qasiteTimesheet
StartTime and EndTime are both datetime type.
Lunch is numeric(9,1).
If I add whole hours e.g 6:00 AM – 5:00 PM it works fine, but add 34, 10, 2 it rounds up incorrectly.
What am I doing wrong?
60.0is decimal(3,2) and the rules for decimal division are quite complex.lunchdecimal(9,1) too, so you can never have more than 6 minute accuracy herePut together, it’s a simply precision problem.
I’m not going to work it out fully (you can see my answer here) so try this, or an explicit CAST, or some combination