A similar article was written earlier, which is used for development
If OBJECT_ID (n ' tf_data_timerange ', n ' FN ') is not Nulldrop function Tf_data_timerangegocreate function tf_data_timerange (@startDate varchar (20),--Start date @enddate varchar (20),--End date @datatype INT--Data type 1: Hours 2: Day) returns @temp table ( Int,monitortime varchar (as/********************************--function): Recursive generation time period--author:zhujt--create date:2015-5-28 17:07:11*********************************/beginif @dataType =1 beginwith Temp (orderby,vdate) as ( Select 1 orderby,convert (varchar), @startDate, +) union ALL Select Orderby+1, convert (varchar), DATEADD (hour,1, vdate) from temp where vdate < @endDate) insert into @temp (orderby,monitortime) Select Orderby,vdate from TEM Poption (maxrecursion 0)--exclusion limit value Endelse if @dataType =2begin set @endDate =convert (varchar), @endDate, +); with temp ( orderby,vdate) as (select 1 orderby,convert (varchar), @startDate,) union ALL Select Orderby+1, convert (varchar (10) , DateAdd (Dd,1,vdate), () from temp where Vdate < @endDaTE) insert into @temp (orderby,monitortime) Select Orderby,vdate from Tempoption (maxrecursion 0)--exclusion limit value End Return;end
SQL Server generation time range