[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

ORACLE 9I SQL QUERY DATE FIELD IN LAST FULL MONTH

Posted on 2007-07-25
3
Medium Priority
?
1,866 Views
Last Modified: 2013-12-19
Hello
Is it possible to retrieve data where the date field selected will always be in the previous month - eg,
for June I only want May's data and for July I only want June' s data etc . So, whenever my query is run I always get previous months data.
What is the syntax for this?
Regards
Phil
0
Comment
Question by:philsivyer
  • 2
3 Comments
 
LVL 22

Expert Comment

by:Helena Marková
ID: 19564234
You can use this:
select t.dat_column from t_table t
where to_number(to_char(sysdate,'mm'))-1=to_number(to_char(t.dat_column,'mm'));
0
 
LVL 8

Accepted Solution

by:
YANN0S earned 500 total points
ID: 19564326
Henka: What If sysdate is 1/1/2008?  to_number(to_char(sysdate,'mm'))-1 will return 0 not 12.

philsivyer: If you don't have any performance issues you can use the following expression:

TRUNC(t.dat_column,'MONTH') = ADD_MONTHS(TRUNC(SYSDATE,'MONTH'),-1)

If you have performance issues (and you have an index on dat_column you can use

t.dat_column >=  ADD_MONTHS(TRUNC(SYSDATE,'MONTH),-1)
AND t.dat_column < TRUNC(SYSDATE,'MONTH')
0
 
LVL 22

Assisted Solution

by:Helena Marková
Helena Marková earned 500 total points
ID: 19564334
Thanks YANNOS :)
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.
Suggested Courses

872 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