本文介紹了確定性標量函數,用于獲取日期的一年中的第幾周的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
限時送ChatGPT賬號..
這里是如何獲取星期幾的好方法,確定性標量函數獲取日期的星期幾.
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)
這篇關于確定性標量函數,用于獲取日期的一年中的第幾周的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!