SQLServer generation time range; sqlserver generation range

Source: Internet
Author: User

SQLServer generation time range; sqlserver generation range
I wrote a similar article earlier, which is used in development.

If OBJECT_ID (n'tf _ Data_TimeRange ', n'fn') is not nullddrop function tf_Data_TimeRangegocreate function tf_Data_TimeRange (@ startDate varchar (20), -- start date @ endDate varchar (20 ), -- end date @ dataType int -- Data Type 1: hour 2: Day) returns @ temp table (orderby int, MonitorTime varchar (20 )) as/******************************** -- function: recursive Generation Time Period -- author: zhujt -- create date: 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) -- exclude limit endelse 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 tempOPTION (MAXRECURSION 0) -- exclude limit end return; end

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.