日期:2014-05-17  浏览次数:20494 次

求指定年份所有的周五
求一个存储过程,查询指定年份中的所有周五~!

------解决方案--------------------
declare @time datetime
set @time='2013/1/1'
select dateadd(day,number,@time) from master..spt_values where type='p' and number between 0 and 365 and year(dateadd(day,number,@time))=year(@time) and Datepart(weekday,dateadd(day,number,@time))=6

------解决方案--------------------
if OBJECT_ID('Pro_test') is not null
drop proc Pro_test
go
create proc pro_test
(
@Year smallint --指定的年份
)
as
--首先需要的是生成这一年的指定日期
;with t
as(
select DATEADD(DD,number,CONVERT(datetime,ltrim(@Year)+'-01-01')) AS dt
from master..spt_values where type='p' 
and number between 0 and datediff(DD,CONVERT(datetime,ltrim(@Year)+'-01-01'),CONVERT(datetime,ltrim(@Year)+'-12-31'))
)
select * from t where DATEPART(WEEKDAY,dt)=6
go

exec pro_test 2013

/*
dt
--------------------------------------
2013-01-04 00:00:00.000
2013-01-11 00:00:00.000
2013-01-18 00:00:00.000
2013-01-25 00:00:00.000
2013-02-01 00:00:00.000
2013-02-08 00:00:00.000
2013-02-15 00:00:00.000
2013-02-22 00:00:00.000
2013-03-01 00:00:00.000
2013-03-08 00:00:00.000
2013-03-15 00:00:00.000
2013-03-22 00:00:00.000
2013-03-29 00:00:00.000
2013-04-05 00:00:00.000
2013-04-12 00:00:00.000
2013-04-19 00:00:00.000
2013-04-26 00:00:00.000
2013-05-03 00:00:00.000
2013-05-10 00:00:00.000
2013-05-17 00:00:00.000
2013-05-24 00:00:00.000
2013-05-31 00:00:00.000
2013-06-07 00:00:00.000
2013-06-14 00:00:00.000
2013-06-21 00:00:00.000
2013-06-28 00:00:00.000
2013-07-05 00:00:00.000
2013-07-12 00:00:00.000
2013-07-19 00:00:00.000
2013-07-26 00:00:00.000
2013-08-02 00:00:00.000
2013-08-09 00:00:00.000
2013-08-16 00:00:00.000
2013-08-23 00:00:00.000
2013-08-30 00:00:00.000
2013-09-06 00:00:00.000
2013-09-13 00:00:00.000
2013-09-20 00:00:00.000
2013-09-27 00:00:00.000
2013-10-04 00:00:00.000
2013-10-11 00:00:00.000
2013-10-18 00:00:00.000
2013-10-25 00:00:00.000
2013-11-01 00:00:00.000
2013-11-08 00:00:00.000
2013-11-15 00:00:00.000
2013-11-22 00:00:00.000
2013-11-29 00:00:00.000
2013-12-06 00:00:00.000
2013-12-13 00:00:00.000
2013-12-20 00:00:00.000
2013-12-27 00:00:00.000
*/