Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2016-07-28
10
Medium Priority
?
64 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 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 78

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
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
LVL 78

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 78

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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
Via a live example, show how to take different types of Oracle backups using RMAN.

916 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