SQL – Retrieve Last X Days Records

Scenario: I want to retrieve all the records in a table which were added in the last 7 days. When a record is added the days date is recorded in the record as Date_Added field.

Solution:

SELECT * FROM Table1
WHERE date_added >= dateadd(day,datediff(day,0,GetDate())- 7,0)

Where 7 could be any integer about how many days you would retain after the selection query. :)

>= dateadd(day,datediff(day,0,GetDate())- 7,0)
SOURCE

LINK

LANGUAGE
ENGLISH