更新时间:2023-09-22 16:03:40
您可以使用DATEPART来确定当前日期是星期几.下面是演示它的代码.
You can use DATEPART to determine what week the current date is. Below is a code that demonstrates it.
DECLARE @currentDayOfWeek AS INT
DECLARE @currentDate AS DATETIME
DECLARE @startDate AS DATETIME
SET @currentDayOfWeek = (DATEPART(dw, GETDATE()) - 1) * -1
SET @currentDate = CAST(CAST(MONTH(GETDATE()) AS VARCHAR(2)) + '/'
+ CAST(DAY(GETDATE()) AS VARCHAR(2)) + '/'
+CAST(YEAR(GETDATE()) AS VARCHAR(4)) AS DATETIME)
SET @startDate = DATEADD(DW,@currentDayOfWeek,@currentDate)
SELECT YEAR(@currentDate) AS [YEAR],
DATEPART(WW, @currentDate) AS WEEK,
@startDate AS STARTDATE,
DATEADD(DW, 6, @startDate) AS ENDDATE
输出为
Output is
YEAR WEEK STARTDATE ENDDATE
2011 13 03/20/2011 03/26/2011
请注意,您的周数是13,因为它计算的是全年的周数.如果您希望它按月而不是按年计算周数,则需要做一些减法运算.
这不是一个很好的解决方案,但是它可以让您抢先一步. :)
Note that you week is 13 because it computes for the week number for the whole year. You need to do a little subtraction if you want it to do the week numbers by month and not by year.
Not an elegant solution but it can give you a headstart on what you need to do. :)
这是链接
sriramjithendra.blogspot.com
here is the link
sriramjithendra.blogspot.com