alex paolo alex paolo - 5 months ago 13
SQL Question

How to minus 2 month in SQL Server

My requirement is to make this query :

SELECT DATEADD(m, DATEDIFF(m, 0, GETDATE()), 0)


Minus by 2 months and the date should be stayed in 1.

Answer

Add DATEADD function on top of your existing query

SELECT DATEADD(MONTH,-2,DATEADD(M, DATEDIFF(M, 0, GETDATE()), 0)) --2016-05-01 00:00:00.000

If you are using SQL SERVER 2012+ the use EOMONTH function

SELECT DATEADD(DAY,1,EOMONTH(GETDATE(),-3)) --2016-05-01