Presentation is loading. Please wait.

Presentation is loading. Please wait.

Creating Functions This presentation was prepared by Professor Steve Ross, with the advice of other MIS Faculty, for use in MIS Classes at Western Washington.

Similar presentations


Presentation on theme: "Creating Functions This presentation was prepared by Professor Steve Ross, with the advice of other MIS Faculty, for use in MIS Classes at Western Washington."— Presentation transcript:

1 Creating Functions This presentation was prepared by Professor Steve Ross, with the advice of other MIS Faculty, for use in MIS Classes at Western Washington University. Please contact Dr. Ross for permission to use in other settings. Further modifications by Kraig Pencil.Dr. Ross

2 The Challenge Create a function that displays a person’s name in a single field: –Ex.: Last, First Middle –Ex.: First Middle Last The function should accommodate possibility of missing or null First, Middle or Last names

3 Design Before You Build What input fields – from what tables – are required? –tblPerson LastName – Data Type: variable character length (size) FirstName – Data Type: variable character length (size) MidName – Data Type: variable character length (size) What is the output and how should the output? For First Middle Last  –Include a space after the first or middle name (if there is one) For Last, First Middle  –Include a comma after the last name only when followed by non-blank first name.

4 Creating the Function, Step 1 Functions are contained under Programmability in the database –Scalar-valued Functions return a single value (not a table of values) –We will create a Scalar-valued Function by typing the SQL declaration in a query script window:

5 Example of a Function Definition CREATE FUNCTION ufnEqualOrNot ( -- Declare your function parameters, e.g. … @input1 varchar(30), @input2 varchar(30) ) RETURNS varchar(60) AS BEGIN -- Declare any additional variables needed DECLARE @Result varchar(60) -- Add your program logic (“code”) to compute the return value If @input1 <> @input2 SELECT @Result = 'not equal' Else SELECT @Result = 'equal' -- Return the result of the function RETURN @Result END GO Use CREATE FUNCTION to create the function. Use ALTER FUNCTION to alter the function.

6 “Clean Up Your Data” with RTRIM & LTRIM Problems can arise when the data (names) contain spaces, Null, or empty strings -- Given the following data, what will the resulting string be? SELECT @FirstName = ' Kraig ' SELECT @MiddleName = NULL SELECT @LastName = ' Pencil ' ---------------------------------------------------------------------------------------------------------------------------------------------------- SELECT @FullName = (@FirstName + @LastName)The result: ' Kraig Pencil ' SELECT @FullName = RTRIM(LTRIM(@FirstName + @LastName)) The result: 'Kraig Pencil' SELECT @FullName = RTRIM(LTRIM(@FirstName)) + RTRIM(LTRIM(@LastName)) The result: 'KraigPencil' SELECT @FullName = RTRIM(LTRIM(@FirstName)) + RTRIM(LTRIM(@MiddleName)) The result: + RTRIM(LTRIM(@LastName)) NULL

7 Bulletproof Your Code This code handles problems that can arise when the data contain spaces, Null or empty strings BEGIN -- Note: '' is two single quotation marks. ' ' has a space between the quotes. -- This variable will hold the fullname DECLARE @FullName varchar(75) -- Set @FullName to '', otherwise it will contain NULL SELECT @FullName = '' IF (@FirstName IS NOT NULL AND @FirstName<> '') SELECT @FullName = @FullName + RTRIM(LTRIM(@FirstName)) -- Add [space] and middle name IF (@MidName IS NOT NULL AND @MidName <> '') SELECT @FullName = @FullName + ' ' + RTRIM(LTRIM(@MidName)) -- Add [space] and last name IF (@LastName IS NOT NULL AND @LastName <> '') SELECT @FullName = @FullName + ' ' + RTRIM(LTRIM(@LastName)) -- @FullName could have a leading space. How? RETURN RTRIM(LTRIM(@FullName)) END

8 Testing the Function I Open a New Query window Type a simple SELECT statement Execute the statement

9 Testing the Function II SELECT statement to refer to fields from a table Execute the statement Note: No leading spaces


Download ppt "Creating Functions This presentation was prepared by Professor Steve Ross, with the advice of other MIS Faculty, for use in MIS Classes at Western Washington."

Similar presentations


Ads by Google