2

这是获取日期的星期几的好方法,确定性标量函数获取日期的星期几

现在,任何人都可以帮我创建一个确定性的标量函数来获取一年中的一周吗?谢谢。

4

1 回答 1

3

这具有确定性,我可以将其用作计算列。

datediff(week, dateadd(year, datediff(year, 0, @DateValue), 0), @DateValue) + 1 

测试代码:

;
with 
Dates(DateValue) as 
(
    select cast('2000-01-01' as date)
    union all 
    select dateadd(day, 1, DateValue) from Dates where DateValue < '2050-01-01'
)
select 
    year(DateValue) * 10000 + month(DateValue) * 100 + day(DateValue) as DateKey, DateValue,        
    datediff(day, dateadd(week, datediff(week, 0, DateValue), 0), DateValue) + 2 as DayOfWeek,
    datediff(week, dateadd(month, datediff(month, 0, DateValue), 0), DateValue) + 1 as WeekOfMonth,
    datediff(week, dateadd(year, datediff(year, 0, DateValue), 0), DateValue) + 1 as WeekOfYear
    from Dates option (maxrecursion 0)
于 2012-07-11T13:11:19.523 回答