Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

compare date logic Month/Year to filter on last 5 years data

Posted on 2016-07-28
10
Medium Priority
?
61 Views
Last Modified: 2016-07-28
I want to add a flag in my script which will compare Month/Year of my date field with the sysdate Month/Year to filter on last 5 years data.

This was my in'tial script:

SELECT
       e.EMP
       e.emp_NAME,
       e.hire_date,
       e.TERM_DATE,
       case when (Extract(YEAR from e.term_date) < Extract(YEAR from sysdate) - 5)
                then 1 else 0 end as FLAG
FROM  
    Emp e

how do i change my logic to compare month and Year both? do i add another condition in case statement with Extract Month? But if i do that, i won't be able to compare that with last 5 years.
0
Comment
Question by:need_solution
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 3
  • +1
10 Comments
 
LVL 35

Accepted Solution

by:
Mark Geerlings earned 1000 total points
ID: 41733368
Oracle supports the "add_month" operator that you can use with a negative value to look back any number of months, like 60 to look back five years.

Try this:

SELECT
        e.EMP
        e.emp_NAME,
        e.hire_date,
        e.TERM_DATE,
        case when add_months(e.term_date - 60) < add_months(sysdate - 60)
                 then 1 else 0 end as FLAG
 FROM  
     Emp e
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 41733371
months_between(sysdate,e.term_date)/12 will give you the number of years.

Something like this maybe:
case when months_between(sysdate,e.term_date)/12 <= 5 then 1 else 0 end as FLAG

I'm not understanding your requirement so if you can provide some sample data and expected results it would help a lot.
0
 
LVL 32

Expert Comment

by:awking00
ID: 41733385
Does this work for you?
case when to_date(to_char(e.term_date,'yyyymm'),'yyyymm') < to_date(to_char(add_months(sysdate,-60),'yyyymm'),'yyyymm')
then 1 else 0 end as flag
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 41733394
>> to_date(to_char(add_months(sysdate,-60),'yyyymm'),'yyyymm')

No need to go from date to char back to date.  Just use trunc to remove add info to the desired level.

If the first of the month:
trunc(add_months(sysdate,-60),'mm')
0
 

Author Comment

by:need_solution
ID: 41733396
We want to delete all the term records which are more than 5 years old and retain everything within 5 years. And this report will be run twice a year so we want to look at the month and the year and not just year. Makes sense?
0
 

Author Comment

by:need_solution
ID: 41733402
I guess this worked for me

case when  (add_months(term_date, 0) < add_months(sysdate, -60)) then 1 else 0 end as flag

So all the term_dates within last 5 years are flagged as 0 and everything more than 5 years are flagged as 1.
0
 
LVL 32

Assisted Solution

by:awking00
awking00 earned 500 total points
ID: 41733407
I really didn't understand the original question.
where e.term_date < add_months(sysdate,-60) should be sufficient.
0
 
LVL 32

Expert Comment

by:awking00
ID: 41733409
No need to add 0 months to term_date
0
 
LVL 77

Assisted Solution

by:slightwv (䄆 Netminder)
slightwv (䄆 Netminder) earned 500 total points
ID: 41733411
>>We want to delete all the term records

Then I'm not seeing the need for the CASE statement in the select.

Just delete and add whichever of the above examples you want to the where clause.

For example:
delete from your table where term_date < add_months(trunc(sysdate),-60);

Does your term_date filed have times with them?  If so, we might need to tweak it some.
0
 

Author Closing Comment

by:need_solution
ID: 41733417
Thank you everyone for your help!
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question