Oracle; dates- looking for previous month

Posted on 2006-04-13
Last Modified: 2008-01-09
Hi Experts
I have this statement
(case to_char(trunc(sysdate,'mm'),'d') when '7' then trunc(sysdate,'mm')+2 when '1' then trunc(sysdate,'mm')+1 else trunc(sysdate,'mm') end) NEWDATE
from dual

That brings me back to the first business day of the month however  I want to add something to this that would bring me back to the first business day of the PREVIOUS month when May 1st rolls around- or any new month actually.
So when May 1st comes- and sysdate is may 1st, I want this sql to be able to go back to April 3rd, which is the first business day of April.  The same would apply for June, when June 1st comes around- i want this sql to bring the date back to May 1st which would be the first busness day of that month.
MY end in mind is to create something that I can add to scripts that I have running in oracle that would allow me to schedule them so that I do not have to change the date every new month.


Question by:jaw1971
    LVL 7

    Accepted Solution

    (case to_char(trunc(add_months(sysdate,-1),'mm'),'d') when '7' then trunc(add_months(sysdate,-1),'mm')+2 when '1' then trunc(add_months(sysdate,-1),'mm')+1 else trunc(add_months(sysdate,-1),'mm') end) NEWDATE
    from dual
    LVL 4

    Expert Comment

    Use the add month function

    select trunc( add_months(sysdate, -1), 'MM') from dual.

    This will give you the idea..

    Then replace all sysdates in your query with addmonths(sysdate, -1).

    LVL 1

    Author Comment

    thanks- this is what I wanted

    Featured Post

    Courses: Start Training Online With Pros, Today

    Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

    Join & Write a Comment

    Suggested Solutions

    Introduction A previously published article on Experts Exchange ("Joins in Oracle", makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
    From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
    This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
    Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

    754 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

    18 Experts available now in Live!

    Get 1:1 Help Now