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?
SELECT * FROM product WHERE pdate >= DATEADD(day, -30, getdate()).
In SQL Server, you can use the DATEADD() function to get last 3 months (or n months) records.
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());
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