Solved

Oracle 10g Select older than current month

Posted on 2012-03-19
6
467 Views
Last Modified: 2012-03-19
Experts,

I am trying to select records where the date is older than the current month. I am using this:

and MY_DATE <= ADD_MONTHS(TRUNC(SYSDATE), -1)

but it is not working good. Seems to select records based on 30 days, and not the month.
For example on March 19, any record Feburary or older needs to be selected. The above is eliminating records in late Feb.

How do i do this properly?
0
Comment
Question by:JDCam
  • 3
  • 2
6 Comments
 
LVL 73

Accepted Solution

by:
sdstuber earned 500 total points
ID: 37738886
and MY_DATE < TRUNC(SYSDATE,'MM')
0
 

Author Closing Comment

by:JDCam
ID: 37738896
Works great. Thank you.
0
 
LVL 16

Expert Comment

by:Wasim Akram Shaik
ID: 37738915
then try this

select add_months(trunc(to_date(SYSDATE),'MM'),-1) from dual
0
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
LVL 16

Expert Comment

by:Wasim Akram Shaik
ID: 37738921
sorry... i was late...!! and didn't refresh my window...
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 37738958
wasimibm,

the asker already has the answer, but for your benefit and future readers,  

This has multiple issues:

add_months(trunc(to_date(SYSDATE),'MM'),-1)

first - to_date(sysdate)   -  this doesn't make sense.  SYSDATE is already a date, why would you implicitly convert it to a string and then explicitly convert it back to a date and without a format?  At best it's inefficient, at worst it'll return a corrupted value without error, although it could also return an error and simply fail.

the trunc part is correct assuming you get a valid date out of the first part (not guaranteed though)

the add_months would eliminate all of the previous month as well as the current month.
So, in the asker's example,  March sysdate should return February and older data.  Using add_months on the already truncated value won't work though because that would return January and older data
0
 
LVL 16

Expert Comment

by:Wasim Akram Shaik
ID: 37739235
agreed sdstuber, realised it after it posted the first comment, but was unable to modify it as i had posted some other comment at the same time....

thanks for the clarification..!!!
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.

Question has a verified solution.

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
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…

825 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