IF and else IF : If « Transact SQL « SQL Server / T-SQL

Home
SQL Server / T-SQL
1.Aggregate Functions
2.Analytical Functions
3.Constraints
4.Cursor
5.Data Set
6.Data Type
7.Database
8.Date Timezone
9.Index
10.Insert Delete Update
11.Math Functions
12.Select Query
13.Sequence
14.Store Procedure Function
15.String Functions
16.Subquery
17.System
18.Table
19.Table Joins
20.Transact SQL
21.Transaction
22.Trigger
23.View
24.XML
SQL Server / T-SQL » Transact SQL » If 
IF and else IF


1>
2CREATE 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>
2SELECT dbo.fnFirstName('Washington, George', 'First')
3> GO

----------------------------------------------------------------------------------------
George

(rows affected)
1SELECT dbo.fnFirstName('Washington, George', 'Last')
2> GO

----------------------------------------------------------------------------------------
Washington

(rows affected)
1>
2> drop function fnFirstName
3> GO
1>
2>
3>
           
       
Related examples in the same category
1.Conditional Logic IF
2.ELSE: execute another line of script when the condition is not met
3.IF and EXISTS function
4.If statement
5.If else statement
6.if and else if statement
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.