如何使用SQL Server中的周数获取周开始日期和结束日期?

时间:2017-08-08 06:21:15

标签: sql sql-server date stored-procedures

我希望使用周数

获取周开始日期和周结束日期

例如

输入

我的周数= 32

输出

WeekStartingDate     WeekEndingDate
------------------------------------
06-08-2017           12-08-2017

注意

没有日期。只能使用周数。有可能吗?

更新

Date Output

1 个答案:

答案 0 :(得分:1)

我试过这样......

 DECLARE @WK INT  SET @WK = 32
    DECLARE @YR INT  SET @YR = 2017

    SELECT DATEADD (WEEK, @WK, DATEADD (YEAR, @YR-1900, 0)) - 4 -
             DATEPART(DW, DATEADD (WEEK, @WK, DATEADD (YEAR, @YR-1900, 0)) - 4) + 1 AS [WEEK_STARTDATE],

     DATEADD (WEEK, @WK, DATEADD (YEAR, @YR-1900, 0)) - 4 -
             DATEPART(DW, DATEADD (WEEK, @WK, DATEADD (YEAR, @YR-1900, 0)) - 4) + 7 AS [WEEK_ENDDATEDATE]

输出

WEEK_STARTDATE            WEEK_ENDDATEDATE
2017-08-06 00:00:00.000   2017-08-12 00:00:00.000