Home » RDBMS Server » Server Administration » How to select values between today minus 3 months ant today minus 2 months
How to select values between today minus 3 months ant today minus 2 months [message #372428] Tue, 13 February 2001 02:09 Go to next message
Karri Lahtela
Messages: 16
Registered: November 2000
Junior Member
Hi

I need to select values on sales-table where is next condition:
Day between today minus 3 months and today minus 2 months.
Example: I need to select values on sales table where date between february (this month) minus 3month and feb minus 2month.

BR Karri
Re: How to select values between today minus 3 months ant today minus 2 months [message #372431 is a reply to message #372428] Tue, 13 February 2001 03:10 Go to previous message
Balazs VISSY
Messages: 17
Registered: January 2001
Junior Member
The ADD_MONTH function does the trick. For examle, the condition should look like

where datefield between ADD_MONTH(SYSDATE,-3) and ADD_MONTH(SYSDATE,-2)

It gives true value for today (02-13) when the datefield is higher than 2000-11-13 and no more than 2000-12-13.

The only trick you have to be careful with is that it counts EXACTLY 3 and 2 month down the current date, that is:

1. The items on 11-13, but with an earlier hour than the current time won't be returned by the select. (Solve the problem by reseting the SYSDATE by the TRUNC function:

where datefield between ADD_MONTH(TRUNC(SYSDATE),-3) and ADD_MONTH(TRUNC(SYSDATE),-2);

2. If you are intrested in the complete month (eg. november) truncate the SYSDATE to 1th of the month: TRUNC(SYSDATE,'mm');

I hope these help,
Balage
Previous Topic: syntax problems using sqlplus in msdos prompt
Next Topic: performance
Goto Forum:
  


Current Time: Sat Jun 29 00:56:19 CDT 2024