SQLServer產生時間範圍,sqlserver產生範圍
前面寫過類似的文章,這是開發時用的
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 --資料類型 1:小時 2:日) returns @temp table(orderby int,MonitorTime varchar(20))as/********************************--function:遞迴產生時間段--author:zhujt--create date:2015-5-28 17:07:11*********************************/beginif @dataType=1 beginwith 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 tempOPTION (MAXRECURSION 0) --排除限值endelse if @dataType=2begin 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 tempOPTION (MAXRECURSION 0) --排除限值end return;end