SQLServer生成时间范围
发布时间:2020-12-12 13:04:48 所属栏目:MsSql教程 来源:网络整理
导读:前面写过类似的文章,这是开发时用的 if OBJECT_ID(N'tf_Data_TimeRange',N'FN') is not nulldrop function tf_Data_TimeRangegocreate function tf_Data_TimeRange(@startDate varchar(20),--开始日期@endDate varchar(20),--结束日期@dataType int --数据
前面写过类似的文章,这是开发时用的
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,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,convert(varchar(10),dateadd(DD,vdate from temp OPTION (MAXRECURSION 0) --排除限值 end return; end (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |