Skip to Main Content
  • Questions
  • Using EXTRACT function, trying to get previous years data plus this year.

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question, Michael.

Asked: January 10, 2017 - 8:24 pm UTC

Last updated: August 02, 2021 - 10:28 am UTC

Version: Oracle 10

Viewed 10K+ times! This question is

You Asked

I have a database view that is currently getting data using EXTRACT(YEAR FROM SYSDATE) so it only gets me data from this year. I need to get data from the view using the previous year's data plus the first three months of the current year always. Can I use the EXTRACT function to do this?

and Connor said...

SQL> select
  2    extract(year from sysdate) y1,
  3    extract(year from add_months(sysdate,-12)) y2,
  4    extract(year from add_months(sysdate,-3)) y3
  5  from dual;

        Y1         Y2         Y3
---------- ---------- ----------
      2017       2016       2016



Rating

  (3 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Michael Terry, January 12, 2017 - 4:23 pm UTC


A reader, July 24, 2018 - 3:55 pm UTC

Worked for me

current year and previous year till date

Shahnawaz Faridi, August 02, 2021 - 8:02 am UTC

I want to select the data count current year till date and previous year same date to compair them like
from 1jan2021 till current date(02aug2021) and privous year 1jan2020 till 02aug2020 and 1jan2019 till 02aug2019 so on.
Chris Saxon
August 02, 2021 - 10:28 am UTC

Please give an example of the data you're working with (create table + inserts)