Solved

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

Posted on 2016-07-28
10
44 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
  • 3
  • 3
  • 3
  • +1
10 Comments
 
LVL 34

Accepted Solution

by:
Mark Geerlings earned 250 total points
Comment Utility
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 76

Expert Comment

by:slightwv (䄆 Netminder)
Comment Utility
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 31

Expert Comment

by:awking00
Comment Utility
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
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
Comment Utility
>> 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
Comment Utility
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:need_solution
Comment Utility
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 31

Assisted Solution

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

Expert Comment

by:awking00
Comment Utility
No need to add 0 months to term_date
0
 
LVL 76

Assisted Solution

by:slightwv (䄆 Netminder)
slightwv (䄆 Netminder) earned 125 total points
Comment Utility
>>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
Comment Utility
Thank you everyone for your help!
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Suggested Solutions

Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
Working with Network Access Control Lists in Oracle 11g (part 2) Part 1: http://www.e-e.com/A_8429.html Previously, I introduced the basics of network ACL's including how to create, delete and modify entries to allow and deny access.  For many…
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

743 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now