-1

可能重复:
从 T-SQL 中的周数获取日期

如果我在 SQL 查询中有周数,如何获取日期值。

就像如果我通过 26,它应该给我 2012 年 6 月 24 日。如果我通过 27,我应该得到 07/01/2012

任何帮助将不胜感激 :)

索茨

4

4 回答 4

0

在 SQL Server 中

DECLARE @StartDate DATE, @WeekVal INT
SET @WeekVal = 26 -- Set the week number
SET @StartDate = DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()), 0) -- Start of current year

;WITH cte AS (
    SELECT @StartDate AS DateVal, DATEPART(wk, @StartDate) AS WeekVal, 1 AS RowVal
    UNION ALL
    SELECT DATEADD(d, 1, DateVal),  DATEPART(wk, DATEADD(d, 1, DateVal)), RowVal + 1 
    FROM cte WHERE RowVal < 365
)
SELECT MIN(DateVal) StartOfWeek
FROM cte
WHERE WeekVal = @WeekVal
OPTION (MAXRECURSION 365);
于 2012-06-26T23:00:07.003 回答
0

这为您提供了一周的开始和结束日期。[对于 SQL Server]

Declare @week integer set @week = 26
Declare @Year Integer Set @Year = year(getdate())
declare @date datetime

-- ------------------------------------
Set @date = DateAdd(day, 0, 
            DateAdd(month, 0, 
            DateAdd(Year, @Year-1900, 0)))

set @date = Dateadd(week, @week-1, @date)

select @date startweek, DATEADD (D, -1 * DatePart (DW, @date) + 7, @date) endweek

这是它的结果:

startweek               endweek
----------------------- -----------------------
2012-07-01 00:00:00.000 2012-07-07 00:00:00.000

(1 row(s) affected)
于 2012-06-27T02:12:38.607 回答
0
SELECT DATEADD(week, n, '11/25/2011');

n 是周数

于 2012-06-26T23:14:28.143 回答
0

如果这不起作用,请尝试使用WEEK()而不是WEEKOFYEAR().

CURDATE() - INTERVAL WEEKDAY(CURDATE()) DAY + INTERVAL (WEEKNO - WEEKOFYEAR(CURDATE())) WEEK
于 2012-06-26T23:14:57.263 回答