Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

last weeks dates

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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
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…

721 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