Transact SQL MSSQL

1>
2> CREATE FUNCTION fnFirstName (@FullName VarChar(100)
3>                          , @FirstOrLast VarChar(5))
4>   RETURNS VarChar(100)
5> AS
6>   BEGIN
7>     DECLARE @CommaPosition Int
8>     DECLARE @TheName VarChar(100)
9>     IF @FirstOrLast = 'First'
10>        BEGIN
11>           SET @CommaPosition = CHARINDEX(',', @FullName)
12>           SET @TheName = SUBSTRING(@FullName, @CommaPosition + 2, LEN(@FullName))
13>        END
14>     ELSE IF @FirstOrLast = 'Last'
15>        BEGIN
16>           SET @CommaPosition = CHARINDEX(',', @FullName)
17>           SET @TheName = SUBSTRING(@FullName, 1, @CommaPosition - 1)
18>        END
19>      RETURN @TheName
20>   END
21> GO
1>
2> SELECT dbo.fnFirstName('Washington, George', 'First')
3> GO
----------------------------------------------------------------------------------------
George
(1 rows affected)
1> SELECT dbo.fnFirstName('Washington, George', 'Last')
2> GO
----------------------------------------------------------------------------------------
Washington
(1 rows affected)
1>
2> drop function fnFirstName
3> GO
1>
2>
3>