Solved

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

Posted on 2016-07-28
10
56 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
oracle differnce between two timestamps 5 53
error starting form builder in 11g 2 49
Sorting a SQL script 5 41
Multiple Rows in a table for Separate Columns 12 25
Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious side…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This video shows how to recover a database from a user managed backup
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

752 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