简体   繁体   中英

ADD_MONTHS sql query in oracle 12c

I am trying to implement the following SQL query

select SOURCE_SYSTEM,
       START_DATE,
       OWNER,
       SUMMARY,
       DETAIL 
  from CICUSTEXP.ACTIVITIES 
 where SOURCE_PARENT_ID = '001o0000006P8JH' 
   AND START_DATE >= ADD_MONTHS(SYSDATE, -1) 
 ORDER BY START_DATE DESC;

Here STARTDATE is of type TIMESTAMP(6) . Now I want to get data based on the months that I would be passing, if I want to have data for the last 3 months what should I be passing in here? Or if want to have data for the next 12 months starting from today what should I be passing from here. I am quite confused.

Update: From the Conversations below I have the following query to fetch data for the next 12 months. Not sure if they are correct.

select SOURCE_SYSTEM,
       START_DATE,
       OWNER,SUMMARY,
       DETAIL 
  from CICUSTEXP.ACTIVITIES 
 where SOURCE_PARENT_ID = '001o0000006P8JH' 
   AND START_DATE BETWEEN ADD_MONTHS(SYSDATE, 0) AND ADD_MONTHS(SYSDATE, 12) 
 ORDER BY START_DATE DESC;

And for fetching data for the last 3 months I have the following query.

select SOURCE_SYSTEM,
       START_DATE,
       OWNER,SUMMARY,
       DETAIL 
  from CICUSTEXP.ACTIVITIES 
 where SOURCE_PARENT_ID = '001o0000006P8JH' 
   AND START_DATE BETWEEN ADD_MONTHS(SYSDATE, -3) AND ADD_MONTHS(SYSDATE, 0) 
 ORDER BY START_DATE DESC;

Hi you can try with this:

select SOURCE_SYSTEM,
       START_DATE,
       OWNER,
       SUMMARY,
       DETAIL 
  from CICUSTEXP.ACTIVITIES 
 where SOURCE_PARENT_ID = '001o0000006P8JH' 
   AND START_DATE >= ADD_MONTHS(SYSDATE, -n)
 ORDER BY START_DATE DESC;

In this way will be selected all rows with start date bigger than n month ago.

If you want select all rows until some month in the future:

AND START_DATE <= ADD_MONTHS(SYSDATE, n)

Finally if you want all the rows between two dates

AND START_DATE BETWEEN ADD_MONTHS(SYSDATE, -n) MONTH AND ADD_MONTHS(SYSDATE ,n)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM