Generate all dates between two time

Source: Internet
Author: User

--part of the code that rewrites Liangck is a function--Create a functionCreate functionGeneratetime (@begin_date datetime,    @end_date datetime)returns @t Table(Datedatetime) asbegin     withMaco as    (       Select @begin_date  asDateUnion  All       SelectDate+1  fromMacowhereDate+1 <=@end_date    )    Insert  into @t    Select *  fromMacooption(maxrecursion0); returnEnd Go--Test ExampleSelect *  fromDbo.generatetime ('2009-01-01','2009-01-10') --Run Results/*Date-----------------------2009-01-01 00:00:00.0002009-01-02 00:00:00.0002009-01-03 00:00:00.0002009-01-04 00:00:00.0002009-01-05 00:00:00.0002009-01-06 00:00:00.0002009-01-07 00:00:00.0002009-01-08 00:00:00.0002009-01-09 00:00:00.0002009-01-10 00:00:00.000*/   Go--Second Edition--Create a functionCreate functiongenerateTimeV2 (@begin_date datetime,    @end_date datetime)returns @t Table(Datedatetime) asbegin    Insert  into @t    Select DateAdd(DD, Number,@begin_date) asDate fromMaster.. Spt_valueswhereType='P'  and DateAdd(DD, Number,@begin_date)<=@end_date    returnEnd --Test ExampleSelect *  fromDbo.generatetimev2 ('2009-01-01','2009-01-10')--Run Results/*Date-----------------------2009-01-01 00:00:00.0002009-01-02 00:00:00.0002009-01-03 00:00:00.0002009-01-04 00:00:00.0002009-01-05 00:00:00.0002009-01-06 00:00:00.0002009-01-07 00:00:00.0002009-01-08 00:00:00.0002009-01-09 00:00:00.0002009-01-10 00:00:00.000 (s) affected)*/

Generate all dates between two time

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.