Solved

last weeks dates

Posted on 2014-01-22
3
390 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
[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
3 Comments
 
LVL 16

Assisted Solution

by:Surendra Nath
Surendra Nath earned 100 total points
ID: 39799950
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:
Scott Pletcher earned 400 total points
ID: 39801094
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
ID: 39802445
Thanks Scott - this works a treat
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
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.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

710 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