I have a process that compares two DateTime values together to make sure that Activity occurs after Received time. Because we allow 60 seconds of leeway, I previously had a delete statement with the following where clause.
i.ActivityDateTime < i.ReceivedDateTime
AND
DATEDIFF(ss, i.ActivityDateTime, i.ReceivedDateTime) > 60
This process ran fine for a long time until we received a time many, many years in the past. Since DateDiff returns an integer, the comparison overflowed the value and the process failed.
A fix I have in place now is
i.ActivityDateTime < i.ReceivedDateTime
AND
(
DATEDIFF(yy, i.ActivityDateTime, i.ReceivedDateTime) > 0
OR
DATEDIFF(ss, i.ActivityDateTime, i.ReceivedDateTime) > 60
)
Testing has gone well with this process so far, but my worry is that I am missing something and, in the future, I may have another failure based on some other combination of Activity and Received time.
Has anyone else experienced something similar, and what other possible comparisons can I perform so that I can make sure no issues happen in the future?