Solved

last weeks dates

Posted on 2014-01-22
3
385 Views
Last Modified: 2014-01-23
Hello
I want to be able to select last full weeks data from todays date - so, today being 22/01/2014 then I would like last weeks data from Sunday 12th to 18th Saturday

select *
from mytable
where date ??

Regards
0
Comment
Question by:PHIL Sawyer
3 Comments
 
LVL 16

Assisted Solution

by:Surendra Nath
Surendra Nath earned 100 total points
Comment Utility
check the below code
DECLARE @Dt DATETIME 
SELECT @dt = GETDATE()
select DATEADD(day,CASE DATENAME(weekday,dateadd(dd,-7,@dt))      
            WHEN 'Sunday'       THEN 0
            WHEN 'Monday'       THEN -1
            WHEN 'Tuesday'      THEN -2
            WHEN 'Wednesday'    THEN -3
            WHEN 'Thursday'     THEN -4
            WHEN 'Friday'       THEN -5
            WHEN 'Saturday'     THEN -6
        END,dateadd(dd,-7,@dt))
        ,DATEADD(dd,6,
                        DATEADD(day,CASE DATENAME(weekday,dateadd(dd,-7,@dt))      
                                    WHEN 'Sunday'       THEN 0
                                    WHEN 'Monday'       THEN -1
                                    WHEN 'Tuesday'      THEN -2
                                    WHEN 'Wednesday'    THEN -3
                                    WHEN 'Thursday'     THEN -4
                                    WHEN 'Friday'       THEN -5
                                    WHEN 'Saturday'     THEN -6
                                END,dateadd(dd,-7,@dt))
                                )

Open in new window

0
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 400 total points
Comment Utility
Code below will pick the first Sunday on or before the @base_date + @days_back:

DECLARE @base_date datetime
DECLARE @days_back int
SET @base_date = GETDATE()
SET @days_back = -7

SELECT
    DATEADD(DAY, (DATEDIFF(DAY, 6, @base_date) + @days_back) / 7 * 7, 6) AS start_date,
    DATEADD(DAY, (DATEDIFF(DAY, 6, @base_date) + @days_back) / 7 * 7 + 6, 6) AS end_date
0
 

Author Comment

by:PHIL Sawyer
Comment Utility
Thanks Scott - this works a treat
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

Suggested Solutions

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

763 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

9 Experts available now in Live!

Get 1:1 Help Now