Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL query for getting data for last 3 months

How can you get today's date and convert it to 01/mm /yyyy format and get data from the table with delivery month 3 months ago? Table already contains delivery month as 01/mm/yyyy.

like image 257
user88866050 Avatar asked Feb 20 '14 15:02

user88866050


People also ask

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 get last 30 days data from a table in SQL Server?

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


1 Answers

SELECT *  FROM TABLE_NAME WHERE Date_Column >= DATEADD(MONTH, -3, GETDATE())  

Mureinik's suggested method will return the same results, but doing it this way your query can benefit from any indexes on Date_Column.

or you can check against last 90 days.

SELECT *  FROM TABLE_NAME WHERE Date_Column >= DATEADD(DAY, -90, GETDATE())  
like image 194
M.Ali Avatar answered Oct 02 '22 23:10

M.Ali