PS: This blog post is written using Windows Live Writer 2012, and the format effect may not be good.
In my development process, I encountered a statistical requirement, and the result was a request to return the number of days from the day that the definition input was calculated
For this I have written a table-valued function to address this requirement. Now it is recorded and I hope to help some friends who have encountered such problems.
First we look at the execution:
Returns data for the last 15 days, executing the calling function: SELECT * FROM [Fn_runday] (-15)
The specific functions are as follows:
-- =============================================
--Call: SELECT * FROM [Fn_runday] (-15)
--Create date:2015-01-11
--Description: Get @num days from today's calculation, return to table
-- =============================================
ALTER FUNCTION [dbo]. [Fn_runday] (@num int=-15)
RETURNS @tab TABLE
(
Xsdate VARCHAR (10),
Sort INT
)
As
BEGIN
DECLARE @i INT
SET @i=0
while (@i> @num)
BEGIN
INSERT into @tab
SELECT Xsdae = CONVERT (VARCHAR), DATEADD (Day, @i, GETDATE ()), 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.
Original SQL table-valued functions: Get forward custom days from today's calculation