首页 > 数据库技术 > 详细

SQLServer生成时间范围

时间:2015-05-28 18:03:34      阅读:230      评论:0      收藏:0      [点我收藏+]
前面写过类似的文章,这是开发时用的
if OBJECT_ID(N'tf_Data_TimeRange',N'FN') is not null
	drop function tf_Data_TimeRange
go

create 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

SQLServer生成时间范围

原文:http://blog.csdn.net/mh942408056/article/details/46125811

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!