標籤:top convert table style datetime weight tab arch nbsp
Go--建立函數create function GetWeekDays(@year int)returns @t table (星期天varchar(20))asbegin insert @t select substring(convert(varchar,dateadd(day,x,col),120),1,10) from ( select cast(cast(@year as varchar(4))+‘-1-1‘ as datetime) as col )a cross join ( select top 365 b8.i+b7.i + b6.i + b5.i + b4.i +b3.i +b2.i + b1.i + b0.i x from(select 0 i union all select 1) b0 cross join(select 0 i union all select 2) b1 cross join(select 0 i union all select 4) b2 cross join(select 0 i union all select 8) b3 cross join(select 0 i union all select 16) b4 cross join(select 0 i union all select 32) b5 cross join(select 0 i union all select 64) b6 cross join(select 0 i union all select 128) b7 cross join(select 0 i union all select 256) b8 order by 1 )b where datepart(dw,dateadd(day,x,col))=1 returnend --測試樣本select * from dbo.GetWeekDays(2011) --運行結果/*星期天--------------------2011-01-022011-01-092011-01-162011-01-232011-01-302011-02-062011-02-132011-02-202011-02-272011-03-062011-03-132011-03-202011-03-272011-04-032011-04-102011-04-172011-04-242011-05-012011-05-082011-05-152011-05-222011-05-292011-06-052011-06-122011-06-192011-06-262011-07-032011-07-102011-07-172011-07-242011-07-312011-08-072011-08-142011-08-212011-08-282011-09-042011-09-112011-09-182011-09-252011-10-022011-10-092011-10-162011-10-232011-10-302011-11-062011-11-132011-11-202011-11-272011-12-042011-12-112011-12-182011-12-25 (52 row(s) affected)*/
根據年得到所有星期日的日期