当前位置: 代码迷 >> SQL >> SQLServer生成时间范畴
  详细解决方案

SQLServer生成时间范畴

热度:116   发布时间:2016-05-05 10:21:52.0
SQLServer生成时间范围
前面写过类似的文章,这是开发时用的
if OBJECT_ID(N'tf_Data_TimeRange',N'FN') is not null	drop function tf_Data_TimeRangegocreate function tf_Data_TimeRange(	@startDate varchar(20), --开始日期	@endDate varchar(20),   --结束日期	@dataType int           --数据类型 1:小时 2:日) returns @temp table(orderby int,MonitorTime varchar(20))as/********************************--function:递归生成时间段--author:zhujt--create date:2015-5-28 17:07:11*********************************/begin	if @dataType=1 		begin			with temp(orderby,vdate) as			(select 1 orderby,convert(varchar(20),@startDate,120)			  union all			 select orderby+1, convert(varchar(20),dateadd(HOUR,1,vdate),120) 			   from temp 			  where vdate < @endDate			 ) 			insert into @temp(orderby,MonitorTime)			select orderby,vdate from temp			OPTION (MAXRECURSION 0) --排除限值		end	else if @dataType=2		begin 			set @endDate=convert(varchar(10),@endDate,120);						with temp(orderby,vdate) as			(select 1 orderby,convert(varchar(10),@startDate,120)			  union all			 select orderby+1, convert(varchar(10),dateadd(DD,1,vdate),120) 			   from temp 			  where vdate < @endDate			 ) 			insert into @temp(orderby,MonitorTime)			select orderby,vdate from temp			OPTION (MAXRECURSION 0) --排除限值		end 	return;end

  相关解决方案