SQL Server obtains one months of all dates

Source: Internet
Author: User

SQL Server's custom functions can be divided into three categories, but I only use the above two classes, which can be called scalar functions and table-valued functions, except that the type of the returned data is the difference, and the table-valued function returns a virtual table

The functions of SQL Server are here

For some reason I wrote a function like: Enter the start date and end date, the output is a table, from the open date to the end date of all the days

For example, when I input 20140701,20140708, the output is like this

The code is as follows, please correct me

CeateFUNCTION [dbo].[fn_getdays](@startdateDATE,@enddateDATE)RETURNS TABLE                                                                                 as                                                                                RETURN(                                                                                SELECT DATEADD( Day, Number,@startdate) as Days fromMaster.. Spt_valuesWHERETYPE= 'P'  and DATEADD( Day, Number,@startdate)<= @enddate                                                                                )

Code Explanation:

The following statement is a continuous value from the Sqlservermaster table, the following code can take the continuous value of 1~2047

SELECT  Number  from WHERE = ' P '

This is a SQL Server system function, not much to say

DATEADD (Day,number,@startdate)

Here is another function logic similar to the one above, which is to take 10-minute units of time

CREATE FUNCTION [dbo].[Fn_gettenmis](@startDatetime DATETIME,@endtDateime DATETIME)RETURNS TABLE                                                                                             as                                                                                            RETURN(                                                                                            Select DATEADD(MI, Number*Ten,@startDatetime) asTenmis fromMaster.. Spt_valueswhereType= 'P'  and DATEADD(MI, Number*Ten,@startDatetime)<= @endtDateime                                                                                            )    

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.