# Coupla SQL \[Enterprise Manager\] questions. queries on 'yesterday' or 'last 7 days'

**URL:** <https://boards.straightdope.com/t/coupla-sql-enterprise-manager-questions-queries-on-yesterday-or-last-7-days/357809>\
**Category:** Factual Questions\
**Created:** [May 22, 2006, 8:33pm UTC](https://boards.straightdope.com/t/coupla-sql-enterprise-manager-questions-queries-on-yesterday-or-last-7-days/357809 "2006-05-22T20:33:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lobsang](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/lobsang/32/4067_2.png) [@Lobsang](https://boards.straightdope.com/u/Lobsang)\
**Post date:** [May 22, 2006, 8:33pm UTC](https://boards.straightdope.com/t/coupla-sql-enterprise-manager-questions-queries-on-yesterday-or-last-7-days/357809/1 "2006-05-22T20:33:40Z")

</div>

Q1 how do I write a query, that no matter what day it’s run it will always filter on a date field with yesterday’s date, or the last seven days?  
Q2 Is there a simple way of implementing user input in a query?

In other words could I create a view, which my less tech-savvy colleagues could run in microsoft excel given an account number? and the query would filter by that account number.

---

<div class="post-metadata">

**Author:** ![chrisk](https://avatars.discourse-cdn.com/v4/letter/c/6de8d8/32.png) [@chrisk](https://boards.straightdope.com/u/chrisk)\
**Post date:** [May 22, 2006, 9:03pm UTC](https://boards.straightdope.com/t/coupla-sql-enterprise-manager-questions-queries-on-yesterday-or-last-7-days/357809/2 "2006-05-22T21:03:40Z")

</div>

> [@Lobsang](#):
>
> Q1 how do I write a query, that no matter what day it’s run it will always filter on a date field with yesterday’s date, or the last seven days?

A lot of this can be done using the getdate, dateadd, and datepart functions. For instance:

dateadd(d, -1, getdate()) will return the date and time exactly one day ago from this moment.

where datediff(d, fldDate, getdate()) = 1 will filter for all records where fldDate is yesterday… ie from midnight yesterday, up to one instant before midnight.

where datediff(d, fldDate, getdate()) between 0 and 7 will filter for all records where the date is between one week ago today and today, inclusively.

Hope that these help.
