Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find 2 years back date

Tags:

I want to find today's date but it should be 2 years back. E.g today's date is 6/12/2010 but I want 6/12/2008. How can I do this in SQL server?

like image 572
Amit Patil Avatar asked Dec 06 '10 11:12

Amit Patil


People also ask

How do I get last 30 days records in SQL?

SELECT * FROM product WHERE pdate >= DATEADD(day, -30, getdate()).

How do I get last 3 months data in SQL?

In SQL Server, you can use the DATEADD() function to get last 3 months (or n months) records.


1 Answers

SELECT DATEADD(year, -2, GETDATE())

or

SELECT DATEADD(yy, -2, GETDATE())

or

SELECT DATEADD(yyyy, -2, GETDATE())

If you want to store it as a variable:

DECLARE @twoYearsAgo DATETIME;  SELECT @twoYearsAgo = DATEADD(year, -2, GETDATE()); 
like image 181
Neil Knight Avatar answered Sep 19 '22 18:09

Neil Knight