Solved

last weeks dates

Posted on 2014-01-22
3
386 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
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

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

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
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.

861 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

28 Experts available now in Live!

Get 1:1 Help Now