3

How can I pull data from my table for just the past two years?

Current attempt which isn't recognizing "DATEADD":

select * 
from TABLE 
where EVENTDATE >= DATEADD(year, -1, GETDATE())

4 Answers 4

6

PL/SQL suggests Oracle. The correct logic is:

where eventdate >= sysdate - interval '2' year

Oracle can be finicky about interval arithmetic in general, add_months() is a good habit:

where eventdate >= add_months(sysdate, -24)

Of course, it depends what you mean by "2 year". If you mean since Jan 1st of the last calendar year:

where eventdate >= trunc(sysdate, 'YYYY') - interval '1' year

There is no problem when subtracting a year from January 1st.

7
  • 1
    Not recognizing "DATEADD" (or "GETDATE" for that matter) because they are NOT an Oracle nor a SQL standard functions. Those are strictly(?) MS sql server functions .
    – Belayer
    Nov 14, 2019 at 19:19
  • Thanks for the clarification @Belayer Nov 14, 2019 at 19:50
  • @GordonLinoff I basically want the past 730 days, does the first example do that? Nov 14, 2019 at 19:51
  • @SebastianHubard . . . You can use - interval '730' day if that is what you want. – Nov 14, 2019 at 21:27
  • Need to be careful with sysdate - interval '2' year, it will fail with an exception on a leap day. Nov 22, 2019 at 6:39
3

Assuming Oracle and 2 years as 730 days you can do as simple as:

select *
  from table_name 
  where eventdate >= sysdate -730
;

When you add or subtract numbers from dates, oracle interpret the numbers as days.

Be aware that date is actually a date-time type and sysdate returns the current date-time. So if you are executing this query now at 10 pm it won't get the rows at 9 pm 2 years ago. Only the rows that are 10 pm or later.

You can remove (actually zero) the time of a date type doing trunc(sysdate) -730.

Also, the trunc function has more options. See the documentation.

2
  • Two years is not 720 days, or 730 days for that matter... :) Nov 22, 2019 at 6:41
  • Fixed to 730 days! I assumed 730 days because the asker said it in a comment to another answer. Nov 27, 2019 at 1:37
1

You can use the ADD_MONTHS function to compare to a valid date, 2 years in the past:

select * 
from TABLE 
where EVENTDATE >= ADD_MONTHS(sysdate, -24)

Note: sysdate - interval '2' year will not yield a valid date on leap days (will raise exception ORA-01839), but ADD_MONTHS will give a valid date each time.

0

Following code is ugly but will do the job for you :)

select *
  from TABLE
 where EVENTDATE >= TO_DATE(to_char(SYSDATE, 'YYYY') - 1 || '-' || to_char(SYSDATE, 'MM-DD'),'YYYY-MM-DD')
1
  • This will fail on a leap day. Nov 22, 2019 at 6:40

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

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