Several ways SQL generates a list of times per day of the year

Source: Internet
Author: User

Worked for several years, has not written a blog, ready to pick up ... The following script applies to the environment: SQL SERVER (starting with 2012) 1, Build sequence:/*1-1: Use cross-linking to recommend the following notation */
SELECT/*2012 started with the offset syntax and is no longer recommended for top N*/s1.i+s2.i+s3.i+S4.i+S5.i+S6.i+S7.i+S8.i+S9.i asseq from(SELECTI= 0 UNION  All SELECTI= 1) S1 Cross JOIN(SELECT 0  asIUNION  All SELECT 2  asi) S2 Cross JOIN(SELECT 0  asIUNION  All SELECT 4  asi) S3 Cross JOIN(SELECT 0  asIUNION  All SELECT 8  asi) S4 Cross JOIN(SELECT 0  asIUNION  All SELECT  -  asi) S5 Cross JOIN(SELECT 0  asIUNION  All SELECT  +  asi) S6 Cross JOIN(SELECT 0  asIUNION  All SELECT  -  asi) S7 Cross JOIN(SELECT 0  asIUNION  All SELECT  -  asi) S8 Cross JOIN(SELECT 0  asIUNION  All SELECT  the  asi) S9ORDER  bySeqoffset0ROWSFETCH NEXT 366ROWS only

/* 1-2: If the number of system table columns is insufficient or too large, the result or performance will be affected */
SELECTRow_number () Over(ORDER  byA1.object_id)- 1  asseq fromSys.all_columns A1 with(NOLOCK), Sys.all_columns A2 with(NOLOCK)ORDER  bySeqoffset0ROWSFETCH NEXT 366ROWS only

/* 1-3: Looping, deprecated notation */
DECLARE @i INTDECLARE @seq TABLE(SeqINT  not NULL)SET @i=0 while @i < 366BEGININSERT  into @seq(seq)VALUES(@i)SET @i+=1ENDSELECT *  from @seq

2. Based on any of the above sequences, use DATEADD (Day,seq, @DstDate) to generate a time list for each day of the year. 3, expand: How to use the above ideas in MySQL, in the premise of not writing stored procedures or functions, the use of SQL statements to split a string into an array? Click here to transfer to view answers >>>

Several ways SQL generates a list of times per day of the year

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.