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.

Slides:



Advertisements
Similar presentations
Importing Data from a Spreadsheet that contains Phone Numbers, ZIP Codes and other similar string data that looks like a number If you came to this presentation.
Advertisements

AN INTRODUCTION TO PL/SQL Mehdi Azarmi 1. Introduction PL/SQL is Oracle's procedural language extension to SQL, the non-procedural relational database.
PL/SQL. Introduction to PL/SQL PL/SQL is the procedure extension to Oracle SQL. It is used to access an Oracle database from various environments (e.g.
Web Database Programming Connecting Database to Web.
PL/SQL (Procedural Language extensions to SQL) Prepared by: Manoj Kathpalia Edited by: M V Ramakrishna.
Structured Query Language - SQL Carol Wolf Computer Science.
This course has taken from This unique introductory SQL tutorial not only provides easy-to-understand SQL instructions, but it allows.
10 November JavaScript. Presentation Hints What do YOU think makes a good presentation Some of my suggestions Don’t write full sentences on slides Talk,
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 2: Single-Table Selections.
The Java String Type CSC 1401: Introduction to Programming with Java Week 7 – Lecture 1 Wanda M. Kunkle.
Structured Query Language SQL: An Introduction. SQL (Pronounced S.Q.L) The standard user and application program interface to a relational database is.
Module 2: Using Transact-SQL Querying Tools. Overview SQL Query Analyzer Using the Object Browser Tool in SQL Query Analyzer Using Templates in SQL Query.
Creating an ERD in SQL Server If you came to this presentation via a web browser, right-click and choose “Full Screen” before proceeding. Click mouse or.
Chapter 5 Introduction to SQL. Structured Query Language = the “programming language” for relational databases SQL is a nonprocedural language = the user.
CSCI 6962: Server-side Design and Programming JDBC Database Programming.
Copyright © 2003 Pearson Education, Inc. Slide 8-1 The Web Wizard’s Guide to PHP by David Lash.
Creating a New Database If you came to this presentation via a web browser, right-click and choose “Full Screen” before proceeding. Click mouse or press.
2440: 141 Web Site Administration Database Management Using SQL Professor: Enoch E. Damson.
PHP meets MySQL.
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
Oracle10g Developer: PL/SQL Programming1 Objectives Programming fundamentals The PL/SQL block Define and declare variables Initialize variables The NOT.
15/10/20151 PHP & MySQL 'Slide materials are based on W3Schools PHP tutorial, 'PHP website 'MySQL website.
PHP MySQL Introduction. MySQL is the most popular open-source database system. What is MySQL? MySQL is a database. The data in MySQL is stored in database.
Web Scripting [PHP] CIS166AE Wednesdays 6:00pm – 9:50pm Rob Loy.
1 Structured Query Language (SQL). 2 Contents SQL – I SQL – II SQL – III SQL – IV.
Importing Data from a Spreadsheet If you came to this presentation via a web browser, right-click and choose “Full Screen” before proceeding. Click mouse.
LECTURE 1 INTRODUCTION TO PL/SQL Tasneem Ghnaimat.
CS178 Database Management PL/SQL session 8 References: ORACLE 9i PROGRAMMING A Primer Rajshekhar Sunderraman.
What is PHP? PHP stands for PHP: Hypertext Preprocessor PHP is a server-side scripting language, like ASP PHP scripts are executed on the server PHP supports.
Intro to SQL Management Studio. Please Be Sure!! Make sure that your access is read only. If it isn’t, you have the potential to change data within your.
Jessica Bennett, Advance America Barbara Ross, Flexshopper LLC PharmaSUG 2015 Paper #QT06.
Chapter 05 (Part III) Control Statements: Part II.
SQL Fundamentals  SQL: Structured Query Language is a simple and powerful language used to create, access, and manipulate data and structure in the database.
Disk Files for I/O your variable (of type ifstream) your variable (of type ofstream) disk file “myInfile.dat” disk file “myOut.dat” executing program input.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
C Programming – Part 3 Arrays and Strings.  Collection of variables of the same type  Individual array elements are identified by an integer index 
1 Cannon_Chapter9 Strings and the string Class. 2 Overview  Standards for Strings  String Declarations and Assignment  I/O with string Variables 
Creating an Access Project* If you came to this presentation via a web browser, right-click and choose “Full Screen” before proceeding. Click mouse or.
Representing Strings and String I/O. Introduction A string is a sequence of characters and is treated as a single data item. A string constant, also termed.
1 STRINGS String data type Basic operations on strings String functions String procedures.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Importing Data from a Spreadsheet If you came to this presentation via a web browser, right-click and choose “Full Screen” before proceeding. Click mouse.
CST336, Dr. Krzysztof Pietroszek Week 2: PHP. 1.Introduction to PHP 2.Embed PHP code into an HTML web page 3.Generate (output HTML) web page using PHP.
Importing Data from a Spreadsheet that contains Phone Numbers, ZIP Codes and other similar string data that looks like a number If you came to this presentation.
Creating a New Database If you came to this presentation via a web browser, right-click and choose “Full Screen” before proceeding. Click mouse or press.
Creating Functions If you came to this presentation via a web browser, right-click and choose “Full Screen” before proceeding. Click mouse or press space.
Simple Queries DBS301 – Week 1. Objectives Basic SELECT statement Computed columns Aliases Concatenation operator Use of DISTINCT to eliminate duplicates.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
Understand Data Definition Language (DDL) Database Administration Fundamentals LESSON 1.4.
JavaScript Variables. Definition A variable is a "container" for information you want to store. A variable's value can change during the script.
1 Section 1 - Introduction to SQL u SQL is an abbreviation for Structured Query Language. u It is generally pronounced “Sequel” u SQL is a unified language.
1 c6212 Advanced Database and Client Server MS SQL Server 2000 Stored Procedures and Parameters What ? Why ? How ?
JUST BASIC Lessons 6 – 9 Mr. Kalmes.
Chapter 5 Introduction to SQL.
Creating an ERD in SQL Server
Prepare data for importing
Creating a New Database
Section 3.2c Strings and Method Signatures
ISC440: Web Programming 2 Server-side Scripting PHP 3
SQL OVERVIEW DEFINING A SCHEMA
Sirena Hardy HRMS Trainer
ICT Programming Lesson 4:
Convert from Variable Character to Float
Just Basic Lessons 7&8 Mr. Kalmes.
Special Registers, Date functions, Case and User Defined Functions!!
COP 2700 – Data Structures (SQL)
Programming Basics Review
SQL NOT NULL Constraint
Intro to SQL CS-422 Dick Steflik.
Presentation transcript:

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

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

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.

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:

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

“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? = ' Kraig ' = NULL = ' Pencil ' = result: ' Kraig Pencil ' = The result: 'Kraig Pencil' = + The result: 'KraigPencil' = + The result: + NULL

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 varchar(75) -- to '', otherwise it will contain NULL = '' IF IS NOT NULL '') Add [space] and middle name IF IS NOT NULL <> '') + ' ' + -- Add [space] and last name IF IS NOT NULL <> '') + ' ' + could have a leading space. How? RETURN END

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

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