CREATE FUNCTION
Create a new Function
Section titled “Create a new Function”CREATE FUNCTION FirstWord (@input varchar(1000))RETURNS varchar(1000)ASBEGIN DECLARE @output varchar(1000) SET @output = SUBSTRING(@input, 0, CASE CHARINDEX(' ', @input) WHEN 0 THEN LEN(@input) + 1 ELSE CHARINDEX(' ', @input) END)
RETURN @outputENDThis example creates a function named FirstWord, that accepts a varchar parameter and returns another varchar value.
Syntax
Section titled “Syntax”Parameters
Section titled “Parameters”|Argument|Description |---|---|---|--- |function_name|the name of function |list_of_paramenters|parameters that function accepts |return_data_type|type that function returs. Some SQL data type |function_body|the code of function |scalar_expression|scalar value returned by function
Remarks
Section titled “Remarks”CREATE FUNCTION creates a user-defined function that can be used when doing a SELECT, INSERT, UPDATE, or DELETE query. The functions can be created to return a single variable or a single table.