Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find records from previous x days?

Tags:

How can I come up with a stored procedure that selects results for past 30 days?

where MONTH(RequestDate) > 6 and DAY(RequestDate) >= 10  and MONTH(RequestDate) < 21 and DAY(RequestDate) < 7 
like image 704
dexter Avatar asked Jul 21 '11 21:07

dexter


People also ask

How do I get previous day records in SQL?

To get yesterday's date, you need to subtract one day from today's date. Use GETDATE() to get today's date (the type is datetime ) and cast it to date . In SQL Server, you can subtract or add any number of days using the DATEADD() function. The DATEADD() function takes three arguments: datepart , number , and date .

How do I get last 7 days record in SQL?

Here's the SQL query to get records from last 7 days in MySQL. In the above query we select those records where order_date falls after a past interval of 7 days. We use system function now() to get the latest datetime value, and INTERVAL clause to calculate a date 7 days in the past.

How do I get previous 30 days in SQL?

Bookmark this question. Show activity on this post. SELECT * FROM product WHERE pdate >= DATEADD(day, -30, getdate()).


2 Answers

SELECT * FROM Table WHERE GETDATE() >= DATEADD(DAY, -30, GETDATE()) 

Substitute the first GETDATE() with the appropriate column name.

SELECT * FROM Table WHERE Table.ColumnName >= DATEADD(DAY, -30, GETDATE()) 
like image 191
Neil Knight Avatar answered Dec 04 '22 05:12

Neil Knight


Are you looking for last 30 days or last month? To find start and end of each month ("generic" as your comment says), use:

select  dateadd(month,datediff(month,0,getdate()),0),      dateadd(mm,datediff(mm,-1,getdate()),-1) 
like image 42
nathan_jr Avatar answered Dec 04 '22 06:12

nathan_jr