A reader, June 23, 2020 - 12:52 pm UTC
I have a schedule MMDDYYYY HH24:MI:SS format. A guard comes to the door and swipes his card creating an access_history record.
I want to get the difference in minutes from the schedule time and the access_history time. If the difference is+ or -2 minutes I INSERT a status record of okay. If the difference in minutes is > 2 or <= 4 INSERT an early or late record. If the difference is > +5 or - 5 minutes I INSERT a missed record. In short, I only want to do the math on the HH24:MI part of the records only.
June 23, 2020 - 1:08 pm UTC
I have a schedule MMDDYYYY HH24:MI:SS format
You're storing ACCESS_DATE as a DATE. Oracle Database stores these in an INTERNAL format.
How the date appears when you query it is irrelevant. This is controlled by the NLS_DATE_FORMAT settings for the client.
I only want to do the math on the HH24:MI part of the records only.
So what happens if someone is scheduled to start just before midnight, but arrives shortly after? Do you want the difference to be nearly 24 hours?
I suspect not. In which case the solution above still stands:
- Subtract their access date from the schedule date to get the difference in days
- Multiply this up by the number of minutes in a day (1,440) to get the difference in minutes
- <optional>If this comes out more than one day, modulus the result by 1,440 to get just the minutes</optional>
- Use a case expression to convert the outcome of this to OK, early/late, as necessary