2

I am able to fetch values for :

SELECT * FROM table WHERE ATTENDANCE_DATE < SYSDATE;

but no records for

SELECT * FROM table WHERE ATTENDANCE_DATE between SYSDATE and SYSDATE -20;

what is wrong in this?

4
  • what is the datatype of attendance_date? Commented Oct 25, 2015 at 14:03
  • 1
    From the docs: If expr3 < expr2, then the interval is empty. Commented Oct 25, 2015 at 14:09
  • Also remember that SYSDATE has a time component, so if you are running the query at 2:00pm, SYSDATE - 20 will also be from 2:00pm. Often when checking days you will want to use: WHERE attendance_date BETWEEN TRUNC(SYSDATE - 20) AND TRUNC(SYSDATE) Commented Oct 25, 2015 at 14:20
  • using TRUNC, a great tip. Thanks Glen. Commented Oct 25, 2015 at 15:12

2 Answers 2

6

If you re-write the second where clause to what it means, it's obvious.

The expression between SYSDATE and SYSDATE -20 is equivalent to:

where ATTENDANCE_DATE >= sysdate and ATTENDANCE_DATE <= sysdate - 20;

What you mean is:

where ATTENDANCE_DATE between sysdate - 20 and sysdate;

Theoretically the SQL standard defines the symmetric option for between which allows any "order" of values, so that the following would do what you want:

between symmetric SYSDATE and SYSDATE - 20

But unfortunately Oracle does not support this.

Sign up to request clarification or add additional context in comments.

3 Comments

Unfortunately Oracle does not support symmetric .
@LalitKumarB: that's what I wrote.
@a_horse_with_no_name Yes, I see. +1
3

Your where condition is always false. You need to change the order.

SELECT * 
FROM table 
WHERE ATTENDANCE_DATE BETWEEN SYSDATE - 20 AND SYSDATE;

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.