Solved

SQL to get records between start of last week and end of last week

Posted on 2013-12-11
4
9,409 Views
Last Modified: 2013-12-11
Hi chaps

Can someone help me with some SQL please? I need to select records with a "enter_date" field which is between the start of last week and the end of last week.

That is, not within the last 7 days, but within the last week period.

Any one know how to format that select section to do that?

Olly
0
Comment
Question by:stonneway
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 39711973
select * from yourtable
where enter_date >=  dateadd(wk, datediff(wk, 0, getdate()) - 1, 0)  --first day of last week
and enter_date <t dateadd(wk, datediff(wk, 0, getdate()), 0) --first day of this week
0
 
LVL 32

Accepted Solution

by:
Ephraim Wangoya earned 500 total points
ID: 39711983
get rid of type

select * 
from yourtable
where enter_date >=  dateadd(wk, datediff(wk, 0, getdate()) - 1, 0)  --first day of last week
and enter_date < dateadd(wk, datediff(wk, 0, getdate()), 0) --first day of this week

Open in new window

0
 
LVL 19

Expert Comment

by:jss1199
ID: 39712021
WHERE
Datepart(Week,enter_date)=Datepart(week,current_timestamp)-1

Adding the above where statement will get all data with enter_date in the last full week.
0
 
LVL 1

Author Closing Comment

by:stonneway
ID: 39712169
Thanks for that
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question