日期:2014-05-18 浏览次数:20695 次
declare @begin_date datetime,@end_time datetime select @begin_date='2012-04-11',@end_time='2012-04-13' --1 select dateadd(dd,number,@begin_date) from master..spt_values where type='p' and number between 0 and datediff(dd,@begin_date,@end_time) --2每月第2天 select dateadd(mm,number,convert(varchar(8),dateadd(mm,1,@begin_date),120)+'02') from master..spt_values where type='p' and number between 0 and datediff(dd,@begin_date,@end_time) --3当按周重复时,周几就放(这个周几可以是复数) select dateadd(ww,number,@begin_date) from master..spt_values where type='p' and number between 0 and datediff(dd,@begin_date,@end_time) --每周的前三天 select dateadd(dd,number,dateadd(dd,9-datepart(dw,@begin_date),@begin_date)) from master..spt_values where type='p' and number between 0 and datediff(dd,@begin_date,@end_time)
------解决方案--------------------
--1.当按时间段播放时,从几月几号到几月几号 --根据2012-4-11到2012-4-13日,生成以下格式的记录. declare @bd date,@ed date select @bd='2012-4-11',@ed='2012-4-13' select '1' as '文件ID', 'X' as '播放类型', dateadd(d,number,@bd) as '播放日期' from master.dbo.spt_values where type='P' and number<=datediff(d,@bd,@ed) 文件ID 播放类型 播放日期 ---- ---- ---------- 1 X 2012-04-11 1 X 2012-04-12 1 X 2012-04-13 (3 row(s) affected) --2.当按天复复时,每隔几天播放. --根据按每月第2天,生成以下记录 declare @x int select @x=2 select '2' as '文件ID', 'Y' as '播放类型', cast(dateadd(d,number,'2012-01-01') as date) as '播放日期' from master.dbo.spt_values where type='P' and number<=365 and day(dateadd(d,number,'2012-01-01'))=@x and month(dateadd(d,number,'2012-01-01')) in (5,6,7) 文件ID 播放类型 播放日期 ---- ---- ---------- 2 Y 2012-05-02 2 Y 2012-06-02 2 Y 2012-07-02 (3 row(s) affected) --3.当按周重复时,周几就放(这个周几可以是复数) --根据按每周第1,2,3,天,生成以下记录 select '3' as '文件ID', 'N' as '播放类型', cast(dateadd(d,number,'2012-04-15') as date) as '播放日期' from master.dbo.spt_values where type='P' and dateadd(d,number,'2012-04-15') between '2012-04-15' and '2012-04-21' and datepart(dw,dateadd(d,number,'2012-04-15')) in (2,3,4) 文件ID 播放类型 播放日期 ---- ---- ---------- 3 N 2012-04-16 3 N 2012-04-17 3 N 2012-04-18 (3 row(s) affected) --4.按月重复,每月的第几天放. --根据按每周第3天,生成以下记录 declare @x int select @x=3 select '4' as '文件ID', 'N' as '播放类型', cast(dateadd(d,number,'2012-04-01') as date) as '播放日期' from master.dbo.spt_values where type='P' and dateadd(d,number,'2012-04-01') between '2012-04-01' and '2012-04-30' and datepart(dw,dateadd(d,number,'2012-04-15'))=@x 文件ID 播放类型 播放日期 ---- ---- ---------- 4 N 2012-04-03 4 N 2012-04-10 4 N 2012-04-17 4 N 2012-04-24 (4 row(s) affected)