原文转载链接找不着了。
以下问转载内容:
第一种:
--获取当前日期是本月的第几周
--测试:select dbo.getMonthWeek(getdate()) 结果:10月的第2周
CREATE function [dbo].[getMonthWeek](@d datetime)
returns varchar(20)
as
begin
declare @returns varchar(20),
@monthfirstDay datetime,
@firstMondy datetime
select @monthfirstDay=left(convert(varchar,@d,23),7)+'-01'
if not exists(select 1
from master.dbo.spt_values
where type=N'P' and number between 0 and datediff(d,@monthfirstDay,@d)
and datepart(dw,dateadd(d,number,@monthfirstDay))=2)
begin
select @monthfirstDay=dateadd(mm,-1,@monthfirstDay)
end
;with t as(select 'days'=dateadd(d,number,@monthfirstDay)
from master.dbo.spt_values