这里是如何获取星期几的好方法,确定性标量函数获取日期的星期几.
Here is a great way of how to get day of week for a date, Deterministic scalar function to get day of week for a date.
现在,有人可以帮我创建一个确定性标量函数来获取一年中的一周作为日期吗?谢谢.
Now, could anyone help me to create a deterministic scalar function to get week of year for a date please? Thanks.
这是确定性的,我可以将其用作计算列.
This works deterministically, I can use it as a computed column.
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)
这篇关于确定性标量函数,用于获取日期的一年中的第几周的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!