Solved

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

Posted on 2013-12-11
4
10,192 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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
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
Via a live example, show how to shrink a transaction log file down to a reasonable size.

623 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