Solved

SQL - pull data from last week

Posted on 2006-07-20
7
964 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

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.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

840 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