---
title: "How to Write a Query to Get Data from the Last 7 Days in SQL Server?"  
description: "How to Write a Query to Get Data from the Last 7 Days in SQL Server?"  
author: "Ravi Vishwakarma"  
published: 2024-07-16  
updated: 2024-07-16  
canonical: https://www.mindstick.com/forum/160925/how-to-write-a-query-to-get-data-from-the-last-7-days-in-sql-server  
category: "SQL Server"  
tags: ["database", "sql server", "sql server 2008", "sql server 2012", "sql server 2022"]  
reading_time: 1 minute  

---

# How to Write a Query to Get Data from the Last 7 Days in SQL Server?

I need a [query to retrieve](https://www.mindstick.com/interview/33938/write-a-query-to-retrieve-the-total-number-of-employees-in-each-department) [records](https://www.mindstick.com/forum/34640/how-to-create-a-stored-procedure-for-display-all-records) from the `Sales` [table](https://www.mindstick.com/articles/43918/how-to-design-table-using-bootstrap) for the last 7 days. Can someone provide the [SQL query](https://www.mindstick.com/forum/529/rename-table-name-and-column-name-using-sql-query) for this?

## Replies

### Reply by Ravi Vishwakarma

To [retrieve](https://www.mindstick.com/forum/34400/how-to-retrieve-form-values-in-controller-action) data from the last 7 days in [SQL](https://www.mindstick.com/articles/13115/types-of-keys-in-sql-or-oracle-database) Server, you can use the `GETDATE()` function along with the `DATEADD()` function to filter the rows.

Here's a simple example of how to achieve this:

Assume you have a table named `Users` with a column `CreationDate` that stores the date and time when each order was placed.

## [Query](https://www.mindstick.com/blog/202/sub-query-in-sqlserver) to Get Data from the Last 7 Days

```plaintext
SELECT *
FROM Users
WHERE CreationDate >= DATEADD(day, -7, GETDATE());
```

## Explanation

1. `GETDATE()`: This function returns the current date and time.
2. `DATEADD(day, -7, GETDATE())`: This function subtracts 7 days from the current date and time. It essentially calculates the date and time exactly 7 days ago.
3. `OrderDate >= DATEADD(day, -7, GETDATE())`: This condition filters the rows to include only those where the `CreationDate` is within the last 7 days.


---

Original Source: https://www.mindstick.com/forum/160925/how-to-write-a-query-to-get-data-from-the-last-7-days-in-sql-server

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
