简体   繁体   中英

SQL Get interval last week (sunday to saturday)

I need a simple sql script where i can grab the rows with a date between sunday 2 weeks ago and saturday the previous week.

I need the query to return the elements no matter what day of this week I run the query.

Lets say I run the query today: Thursday 12. dec 2016. (12-08-2016)

I need to get this interval:

SELECT * FROM table WHERE date BETWEEN '11-27-2016' AND '12-03-2016'
DECLARE @StartInterval DATE,
        @EndInterval DATE,
        @Today = GETDATE()

SET @EndInterval = DATEADD(dd,-1,DATEADD(dd,-1*(DATEPART(dw,@Today)-1),@Today))
SET @StartInterval = DATEADD(dd,-6,@EndInterval)

SELECT * 
FROM table 
WHERE date BETWEEN @StartInterval AND @EndInterval

您可以使用:从表中选择*,其中date在next_day(date,'SUN')-21和next_day(date,'SAT')-14之间

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM