如何根据sql中的当前日期获取上周的日期范围?

时间:2014-03-31 19:02:15

标签: sql sql-server sql-server-2008

我在水晶报告中有这个代码,它根据当前日期给我上周的日期范围。

一周的第一天:

If DayOfWeek(currentdate) = 2 Then
currentdate
Else If DayOfWeek(currentdate) = 3 Then
dateadd ("d",-1,currentdate)
Else If DayOfWeek(currentdate) = 4 Then
dateadd ("d",-2,currentdate)
Else If DayOfWeek(currentdate) = 5 Then
dateadd ("d",-3,currentdate)
Else If DayOfWeek(currentdate) = 6 Then
dateadd ("d",-4,currentdate)
Else If DayOfWeek(currentdate) = 7 Then
dateadd ("d",-5,currentdate)
Else If DayOfWeek(currentdate) = 1 Then
dateadd ("d",-6,currentdate)

一周的最后一天:

If DayOfWeek(currentdate) = 2 Then
dateadd ("d",+6,currentdate)
Else If DayOfWeek(currentdate) = 3 Then
dateadd ("d",+5,currentdate)
Else If DayOfWeek(currentdate) = 4 Then
dateadd ("d",+4,currentdate)
Else If DayOfWeek(currentdate) = 5 Then
dateadd ("d",+3,currentdate)
Else If DayOfWeek(currentdate) = 6 Then
dateadd ("d",+2,currentdate)
Else If DayOfWeek(currentdate) = 7 Then
dateadd ("d",+1,currentdate)
Else If DayOfWeek(currentdate) = 1 then currentdate

如何在SQL中使用2个变量存储星期一(startdate)和星期日(enddate)?

我在这个网站上发现了这个select datepart(dw,getdate()) --6,但我不知道如何使用它。

4 个答案:

答案 0 :(得分:13)

我在parms CTE中生成了一些间隔日期,然后SELECT CurrentDate parmsCurrentDate之前的星期日和CurrentDate ;WITH parms (CurrentDate) AS ( SELECT DATEADD(dd, -14, CURRENT_TIMESTAMP) UNION SELECT DATEADD(dd, -6, CURRENT_TIMESTAMP) UNION SELECT DATEADD(dd, 2, CURRENT_TIMESTAMP) UNION SELECT DATEADD(dd, 8, CURRENT_TIMESTAMP) UNION SELECT DATEADD(dd, 15, CURRENT_TIMESTAMP) UNION SELECT DATEADD(dd, 20, CURRENT_TIMESTAMP) ) SELECT CurrentDate , LastWeekSunday = DATEADD(dd, -1, DATEADD(ww, DATEDIFF(ww, 0, CurrentDate) - 1, 0)) , LastWeekSaturday = DATEADD(dd, 5, DATEADD(ww, DATEDIFF(ww, 0, CurrentDate) - 1, 0)) FROM parms 之前一周的星期六。我假设你希望dtate范围是周日 - 周六。

  

周日 - 周六范围

;WITH parms (CurrentDate) AS (
    SELECT DATEADD(dd, -14, CURRENT_TIMESTAMP) UNION 
    SELECT DATEADD(dd, -6, CURRENT_TIMESTAMP) UNION
    SELECT DATEADD(dd, 2, CURRENT_TIMESTAMP) UNION
    SELECT DATEADD(dd, 8, CURRENT_TIMESTAMP) UNION
    SELECT DATEADD(dd, 15, CURRENT_TIMESTAMP) UNION
    SELECT DATEADD(dd, 20, CURRENT_TIMESTAMP)
)

SELECT CurrentDate
     , LastWeekMonday   = DATEADD(dd,  0, DATEADD(ww, DATEDIFF(ww, 0, DATEADD(dd, -1, CurrentDate)) - 1, 0))
     , LastWeekSunday   = DATEADD(dd,  6, DATEADD(ww, DATEDIFF(ww, 0, DATEADD(dd, -1, CurrentDate)) - 1, 0))
FROM parms
  

周一至周日范围

SELECT CURRENT_TIMESTAMP
     , LastWeekSunday   = DATEADD(dd,  0, DATEADD(ww, DATEDIFF(ww, 0, DATEADD(dd, -1, CURRENT_TIMESTAMP)) - 1, 0))
     , LastWeekSaturday = DATEADD(dd,  6, DATEADD(ww, DATEDIFF(ww, 0, DATEADD(dd, -1, CURRENT_TIMESTAMP)) - 1, 0))
  

如果您只是希望从前一周的星期一到前一周的星期日,而不是从一列日期开始,您可以使用此

{{1}}

答案 1 :(得分:5)

此解决方案经过测试并正常运行。我将前一周的周一和周日作为上限和下限。

SELECT
    -- 17530101 or 1753-01-01 is the minimum date in SQL Server
    DATEADD(dd, ((DATEDIFF(dd, '17530101', GETDATE()) / 7) * 7) - 7, '17530101') AS [LowerLimit], -- Last Week's Monday
    DATEADD(dd, ((DATEDIFF(dd, '17530101', GETDATE()) / 7) * 7) - 1, '17530101') AS [UpperLimit] -- Last Week's Sunday.

在现实世界的查询中可以这样使用:

SELECT
    *
FROM
    SomeTable
WHERE
    SomeTable.[Date] >= DATEADD(dd, ((DATEDIFF(dd, '17530101', GETDATE()) / 7) * 7) - 7, '17530101') AND
    SomeTable.[Date] <= DATEADD(dd, ((DATEDIFF(dd, '17530101', GETDATE()) / 7) * 7) - 1, '17530101')

以下是一些测试:

<强> 1。闰年

当前日期:2016-02-29 00:00:00.000

结果:

LowerLimit                 UpperLimit
2016-02-22 00:00:00.000    2016-02-28 00:00:00.000

<强> 2。上周是在不同的一年

当前日期:2016-01-06 00:00:00.000

LowerLimit                 UpperLimit
2015-12-28 00:00:00.000    2016-01-03 00:00:00.000

第3。上个月的下限和当月的上限

当前日期:2016-05-04 00:00:00.000

LowerLimit                 UpperLimit
2016-04-25 00:00:00.000    2016-05-01 00:00:00.000

<强> 4。当前日期是星期日

当前日期:2016-05-08 00:00:00.000

LowerLimit                 UpperLimit
2016-04-25 00:00:00.000    2016-05-01 00:00:00.000

答案 2 :(得分:2)

比尔的代码运作良好,但如果我希望根据今天的日期获得上周日到周六,我需要进行一些编辑。

select CURRENT_TIMESTAMP, 
LastWeekSunday   = DATEADD(dd,  -1, DATEADD(ww, DATEDIFF(ww, 0, DATEADD(dd, -1, CURRENT_TIMESTAMP)) - (case datename(weekday,CURRENT_TIMESTAMP) when 'Sunday' then 0 else 1 end), 0)), 
LastWeekSaturday = DATEADD(dd,  5, DATEADD(ww, DATEDIFF(ww, 0, DATEADD(dd, -1, CURRENT_TIMESTAMP)) - (case datename(weekday,CURRENT_TIMESTAMP) when 'Sunday' then 0 else 1 end), 0))

Rgds - Matt

答案 3 :(得分:1)

对于所有相关的日期和日期间隔,我几乎总是建议使用日历表。包含表示日期,星期,月,年,星期几,星期五,星期等的列的表,根据需要预先填充并在每列上编制索引。它每年只有365行。

select min(date), max(date)
from calendar
where week = datepart(week, getdate() - 7)
and year = datepart(year, getdate() - 7)