Solved

SQL - pull data from last week

Posted on 2006-07-20
7
952 Views
Last Modified: 2008-02-01
I need a query that pulls data from last week (Monday through Sunday).  It should not matter what day it is now, if it's Monday, or Friday...  I want to only pull data from last week.  

I tried using r.rundate between GetDate()-7 and GetDate(); but this only pulls the data from the last seven days.  I want to pull all of last week.

Thanks in advance!
Scotto13
0
Comment
Question by:Scotto13
  • 3
  • 3
7 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 17147371
CREATE FUNCTION [dbo].[ufn_GetFirstDayOfWeek] ( @pInputDate DATETIME )
RETURNS DATETIME
BEGIN
    SET @pInputDate = CONVERT(VARCHAR(10), @pInputDate, 111)
    RETURN DATEADD(DD, 1 - DATEPART(DW, @pInputDate), @pInputDate)
END
GO


DECLARE @today datetime
SET @Today = getdate()

SELECT * FROM urTable
WHERE urdateColumn between [dbo].[ufn_GetFirstDayOfWeek] (@today-7) and
 [dbo].[ufn_GetFirstDayOfWeek] (@today-7) + 7
0
 

Author Comment

by:Scotto13
ID: 17147443
Hi Aneesh,

I only have read access to the database, so I am unable to do Create Function.  Can this be simplified - without the function?
0
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 250 total points
ID: 17147542

DECLARE @today datetime, @StartOfWeek datetime, @EndOfWeek Datetime

SET @Today = getdate()
SET @Today = CONVERT(VARCHAR(10), @Today-7, 111)
SET @StartOfWeek = DATEADD(DD, 1 - DATEPART(DW, @Today), @Today)
SET @EndOfWeek  = @StartOfWeek + 6

SELECT @StartOfWeek, @EndOfWeek

SELECT * FROM urTable
WHERE urdateColumn between @StartOfWeek AND @EndOfWeek
0
Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

 

Author Comment

by:Scotto13
ID: 17147618
OK, Aneesh.  I have thre query running in SQL.  Now my solution calls for a SQL Pass-through query in MS Access.  The query won't run in Access.  I can't figure out why.
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 17147638
I have no idea on Access databases, may be you can post in the MS Access area
0
 

Author Comment

by:Scotto13
ID: 17147645
ok.  Thanks for the good solution.
0
 
LVL 25

Expert Comment

by:Mr_Peerapol
ID: 17147673
Try this to combine everything into single statement:

SELECT * FROM urTable
WHERE urdateColumn between DATEADD(DD, 1 - DATEPART(DW, @CONVERT(VARCHAR(10), getdate()-7, 111)
), @CONVERT(VARCHAR(10), getdate()-7, 111)) AND DATEADD(DD, 1 - DATEPART(DW, @CONVERT(VARCHAR(10), getdate()-7, 111)
), @CONVERT(VARCHAR(10), getdate()-7, 111)) + 6
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…
Via a live example, show how to shrink a transaction log file down to a reasonable size.

706 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

20 Experts available now in Live!

Get 1:1 Help Now