Solved

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

Posted on 2016-07-28
10
58 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 250 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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 125 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 125 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Suggested Courses

623 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