'Oracle SQL ability to add filter on Sysdate which accepts negative and posstive values in days
SQL newb here. I'm trying to adjust the filters in my Oracle SQL report, I would like the filter to accept both negative (-) and possitive (+) values.
This is my SQL line:
Work_day <= to_date(sysdate+'&Days_from_current_date')
Problem I'm only able to add + days from the sysdate, I would like to have filter which would accepts:
-XX days from the sysdate
+XX days from the sysdate
Solution 1:[1]
First of all, SYSDATE is a function that returns DATE datatype, so it is wrong to TO_DATE it.
Code you posted should work (there's no evidence it doesn't; could you provide sample data and desired output?).
This is "now":
SQL> select sysdate from dual;
SYSDATE
-------------------
03.03.2022 09:05:04
Query:
SQL> with test (name, work_day) as
2 (select 'Little', date '2022-03-01' from dual union all
3 select 'Foot' , date '2022-03-15' from dual
4 )
5 select *
6 from test
7 where work_day <= trunc(sysdate) + &days_from_current_date;
Enter value for days_from_current_date: 2
NAME WORK_DAY
------ -------------------
Little 01.03.2022 00:00:00
SQL> /
Enter value for days_from_current_date: -1
NAME WORK_DAY
------ -------------------
Little 01.03.2022 00:00:00
SQL> /
Enter value for days_from_current_date: 20
NAME WORK_DAY
------ -------------------
Little 01.03.2022 00:00:00
Foot 15.03.2022 00:00:00
SQL>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Littlefoot |
