Original SQL table-valued functions: Returns date data for a custom time period

Source: Internet
Author: User

Similar to the past, I still introduce a daily development encountered in the knowledge points, I would like to record, but also hope to help some friends.

This time I'm going to go through the SQL function to return the date data that you entered in the two points in time.

As follows:

Execute function: SELECT * FROM [Fn_runselectday] (' 2015-01-01 ', ' 2015-01-11 ')

The specific functions are as follows:

-- =============================================
--Call: SELECT * FROM [Fn_runselectday] (' 2015-01-01 ', ' 2015-01-11 ')
--Create date:2015-01-03
--Description: Returns date data for a custom time period
-- =============================================
CREATE FUNCTION [dbo]. [Fn_runselectday]
(
@begindate DATETIME,
@enddate DATETIME
)
RETURNS @tab TABLE
(
Xsdate VARCHAR (10),
Sort INT
)
As
BEGIN
DECLARE @num INT
SELECT @num = (DATEDIFF (day, @begindate, @enddate) *-1)

DECLARE @i INT
SET @i=0
while (@i>[email protected])
BEGIN
INSERT into @tab
SELECT Xsdae = CONVERT (VARCHAR), DATEADD (Day, @i, @enddate), 23),
Sort = @i
SET @[email protected]
END
RETURN
END

if friends think that may be used later, welcome reprint, of course, can also recommend, thank you.

PS: This blog post is written using Windows Live Writer 2012, and the format effect may not be good.

Original SQL table-valued functions: Returns date data for a custom time period

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.