I have a column that stores the last modified date. I want to check in a SQL query if its value is more than 2 months from the current date.
I know I need to use SYSDATE but am not familiar with date stuff in Oracle so am unsure as to the rest.
MONTHS_BETWEEN returns number of months between dates date1 and date2 . If date1 is later than date2 , then the result is positive. If date1 is earlier than date2 , then the result is negative. If date1 and date2 are either the same days of the month or both last days of months, then the result is always an integer.
The MONTHS_BETWEEN() function is used to get the number of months between dates (date1, date2). See the following conditions: If date1 is later than date2, then the result is positive. If date1 is earlier than date2, then the result is negative.
You are in an Oracle forum, so the Oracle answer is: use add_months(sysdate,-2) to subtract two months from the current date. Regards, Rob.
We can use DATEADD() function like below to Subtract Months from DateTime in Sql Server. DATEADD() functions first parameter value can be month or mm or m, all will return the same result.
SELECT * from table where date_column >= add_months(TRUNC(SYSDATE) + 1, 2);
Here is a query that will give the last 2 month data based on the current date.
SELECT * FROM tableName WHERE created_on >= add_months(sysdate, -2);
try this
SELECT field1,field2 from yourtable where field_date > add_months(SYSDATE, 2);
Bye
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With