Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server Get Date Field if less than 3 month

I just need a where condition in my select statement where AddedDate is less than or within the 3 month backet. Like

Select * FROM My Table where AddedDate DateDiff of DateNow less than 3 months
like image 740
ONYX Avatar asked Jan 30 '12 12:01

ONYX


People also ask

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.

How do I get last 6 months data in SQL?

Instead of approximating the "current" date by selecting the MAX(date) the code could reference CAST(GETDATE() as DATE) to access the system datetime and cast it as type DATE. where [date] > dateadd(month, -6, cast(getdate() as date));

How can I get data between two months in SQL?

For that you should say WHERE my_float >= 1 AND my_float < 3 . Equally, in your case, if you wanted exactly one month, your parameters would both be 20120101 , but there is NO time between those values.


1 Answers

Use DATEADD():

Select * FROM My Table where AddedDate>=DATEADD(m, -3, GETDATE())
like image 76
Curtis Avatar answered Oct 29 '22 02:10

Curtis