?
Solved

Oracle; dates- looking for previous month

Posted on 2006-04-13
3
Medium Priority
?
2,792 Views
Last Modified: 2008-01-09
Hi Experts
I have this statement
select
(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.

thanks
~jw~

0
Comment
Question by:jaw1971
3 Comments
 
LVL 7

Accepted Solution

by:
RainMan82 earned 2000 total points
ID: 16448605
select
(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
0
 
LVL 4

Expert Comment

by:Harish_Rajani
ID: 16448661
Use the add month function

Try
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).

Rgds,
HR
0
 
LVL 1

Author Comment

by:jaw1971
ID: 16468271
Rainman82
thanks- this is what I wanted
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.
Suggested Courses

840 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