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.
The DATE_SUB() function subtracts a time/date interval from a date and then returns the date.
To subtract 30 days from current datetime, first we need to get the information about current date time, then use the now() method from MySQL. The now() gives the current date time. The method to be used for this is DATE_SUB() from MySQL. Here is the syntax to subtract 30 days from current datetime.
If you are using SQL Server try this:
SELECT * FROM MyTable
WHERE MyDate < DATEADD(month, -2, GETDATE())
Based on your update it would be:
SELECT * FROM FB WHERE Dte < DATEADD(month, -2, GETDATE())
Would something like this work for you?
SELECT * FROM FB WHERE Dte >= DATE(NOW() - INTERVAL 2 MONTH);
TSQL, Alternative using variable declaration. (it might improve Query's readability)
DECLARE @gapPeriod DATETIME = DATEADD(MONTH,-2,GETDATE()); --Period:Last 2 months.
SELECT
*
FROM
FB as A
WHERE
A.Dte <= @gapPeriod; --only older records.
SELECT COUNT(1) FROM FB
WHERE Dte > DATE_SUB(now(), INTERVAL 2 MONTH)
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