Define function to get last name : Create Function « Store Procedure Function « SQL Server / T-SQL






Define function to get last name



14>
15> CREATE FUNCTION fnName (@Name VarChar(100))
16>   RETURNS VarChar(100)
17> AS
18>   BEGIN
19>      DECLARE @CommaPosition Int, @LastName varchar(100)
20>      SET @CommaPosition = 6
21>      SET @LastName = SUBSTRING(@Name, 1, @CommaPosition)
22>      RETURN @LastName
23>   END
24> GO
Msg 2714, Level 16, State 3, Server JAVA2S\SQLEXPRESS, Procedure fnName, Line 22
There is already an object named 'fnName' in the database.
1>
2> SELECT dbo.fnName('Washington, George')
3> GO

------------------------------------------------------------------------------------------
Washin

(1 rows affected)
1>
2> drop function fnName
3> GO
           
       








Related examples in the same category

1.Multi-Statement Table-Valued Functions
2.Inline Table-Valued Functions
3.Function Returning a Scalar Value
4.Define user function and use it in select statement