Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

last weeks dates

Posted on 2014-01-22
3
Medium Priority
?
394 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 400 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 70

Accepted Solution

by:
Scott Pletcher earned 1600 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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

916 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