Avatar of mrong
mrong

asked on 

add_months function in oracle

Greeting,

I try to use add_months function to get me all the data for the previous 6 month up to now.
I have select * from tbl where date>=add_months(sysdate,-6)
The above sql will give me the data from Nov 13th to May 13th(Now).
How to get data from Nov 1st to May 13th?

Thanks.
Oracle Database

Avatar of undefined
Last Comment
daddyhaw
ASKER CERTIFIED SOLUTION
Avatar of Sean Stuber
Sean Stuber

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Sean Stuber
Sean Stuber

you could put the trunc on sysdate as well

select * from tbl where date>=add_months(trunc(sysdate,'mm'),-6)


both of these assume you want everything from Nov 1 of previous year up through current day and time, and that there is no future-dated data

if you might have future-dated data, then add an additional clause

AND date <= sysdate
Avatar of Swadhin Ray
Swadhin Ray
Flag of United States of America image

You can also use this:

SELECT * FROM <<TABLE>> WHERE trunc(<<YOUR DATE COLUMN>>,'MM') >= ADD_MONTHS(SYSDATE,-6)
AND <<YOUR DATE COLUMN>> <= sysdate;

Avatar of Sean Stuber
Sean Stuber

putting TRUNC on the column prevents use of most  indexes and isn't recommended
Avatar of Swadhin Ray
Swadhin Ray
Flag of United States of America image

yeah true
Avatar of daddyhaw
daddyhaw
Flag of Jamaica image

select * from tbl where date>='01-'||to_char(add_months(sysdate,-6),'MON-YYYY')

OR

select * from tbl where date>=LAST_DAY(add_months(sysdate,-7))+1
Oracle Database
Oracle Database

Oracle is an object-relational database management system. It supports a large number of languages and application development frameworks. Its primary languages are SQL, PL/SQL and Java, but it also includes support for C and C++. Oracle also has its own enterprise modules and application server software.

81K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo