Solved

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

Posted on 2013-12-11
4
9,680 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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Suggested Solutions

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to shrink a transaction log file down to a reasonable size.

751 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