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 )