Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL query for today's date minus two months

Tags:

sql

sql-server

People also ask

How do I subtract two months from a date in SQL?

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.

How do I subtract a date from today's date in SQL?

The DATE_SUB() function subtracts a time/date interval from a date and then returns the date.

How can get current date minus 30 days in SQL?

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)