Question

I have a table with the following columns

effective_start_date
effective_end_date
person_id

This is on Oracle DB.
I am writing a query to get me the person active for the current sysdate, using the following query.

select startDate, endDate, sysdate
from dateTable
where sysdate between startDate and endDate;

The issue is that this where clause never seems to get applied. The result contains rows that dont satisfy this condition. Any suggestions on what i might be missing.

Was it helpful?

Solution

Your query looks fine, and as APC suggests this is a common pattern. Which means you have a data problem. Displaying the full year will show why the unexpected records are matching, and any 'bad' dates - e.g. using unexpected centuries, which is not an unusual problem when dates are manipulated using a YY format mask:

select to_char(startDate, 'YYYY-MM-DD HH24:MI:SS') as startDate,
    to_char(endDate, 'YYYY-MM-DD HH24:MI:SS') as endDate, sysdate
from dateTable
where sysdate between startDate and endDate;

In the comments you've already noted that this showed dates from 4712, which showed as just 12 using your YY-MON-DD date format mask. I haven't seen exactly that, it's more common to see 0012 or 1912, but a record with endDate in 4712 certainly ends after sysdate. It's probably worth verify all of the dates in the table, not just those that were flagged by that where clause.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top