1

Based on the date_entered column, which is a data_type of 'DATE' how would I parse out the data to pull the time.

Values in the DATE_ENTERED field (DATA_TYPE is DATE) looks like this 5/15/2015 8:43:04 PM

I need to run queries based on when DATE_ENTERED is between 05/15/2015 8:00 AM and 05/15/2015 10:00 AM

I then need to incorporate that into a PL/SQL script that will run every 2 hours with the last two hours as the filter. My best explanation of this would be where it grabs the current date and time, and pulls 2 hours back, every 2 hours. This will allow it to run at 8am, 10am, 12pm, 2pm etc and have the last 2 hours of new entries in the query. For instance 10am run time will include results for items entered in from 8:00am until 10:00am

2
  • 1
    You don't need to "parse out the data to pull the time" -- you can simply subtract the required duration from SYSDATE().
    – mustaccio
    Aug 10, 2015 at 19:52
  • How would I pull the 2 hour duration from SYSDATE? Would it be something along the lines of dateadd(-2, hh,SYSDATE())? How would I do that in Oracle though?
    – Hector
    Aug 10, 2015 at 19:55

1 Answer 1

3

It sounds like you want

WHERE date_entered BETWEEN sysdate - interval '2' hour
                       AND sysdate

Assuming date_entered never contains future dates

WHERE date_entered >= sysdate - interval '2' hour

Depending on exactly what you need (and why you're running the query), you may want to truncate the date to the hour or minute so that you're always comparing against data from the top of the hour

WHERE date_entered >= trunc(sysdate - interval '2' hour, 'HH24')

Depending on why you are doing this, however, keep in mind that you may miss data if you're running every 2 hours. If I insert a row at, say 3:59:59 pm, your job starts runs at 4:00:00 pm (looking for data between 2 and 4) and at 6 pm (looking for data between 4 and 6), and I commit at 4:00:01 pm, then neither execution of the job will return this row.

1
  • Justin, Thank you for your response, I was a little worried I was not 'wording' it correctly and clearly, however I just tried your method and that is exactly what I am looking for. This will be part of a PL/SQL script that will run this query and auto email the results.
    – Hector
    Aug 10, 2015 at 20:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.