Link to home
Start Free TrialLog in
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.
ASKER CERTIFIED SOLUTION
Avatar of Sean Stuber
Sean Stuber

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
You can also use this:

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

putting TRUNC on the column prevents use of most  indexes and isn't recommended
yeah true
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