Solved

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

Posted on 2013-12-11
4
7,808 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
  • 2
4 Comments
 
LVL 32

Expert Comment

by:ewangoya
Comment Utility
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:
ewangoya earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks for that
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now