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

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
Microsoft Office Word 2007 Tutorial
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
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 statement
4. If else statement
5. if and else if statement
w___w___w___._j_av_a_2_s___.___c___o__m___ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.