Special Registers, Date functions, Case and User Defined Functions!!
Agenda Review ISeries installed products Special Registers – System Values Create Tables Date Functions Case Building our own SQL Functions
Review Table vs *FILE Library vs Collection What is a schema? Constraints?
ISeries installed Products Display Software Resources
Build the following Database Students Student Number Student Name Address Enrolled Date Student Marks Student Number Course Code Final Grade
Insert Data
Add 1 year, 2 months and 3 days to enrolled date Select Statement As?
How many days has a student been at Seneca? Select Statement
Case Statements Provides a multiconditional test Eg: select orderid, when totamt <= 100 then ‘Small’ when totamt <= 500 then ‘Medium’ else ‘Large’ end as SalesSize From Sale
Case Statements select orderid, case ShipCity when ‘Toronto’ then ‘Ontario’ when ‘Montreal’ then ‘Quebec’ else ‘Invalid’ end as CustProv From Sale
Write a select statement that produces a student marks report and lists the letter grade
Build your own SQL function First line: Create Function fname Where fname is the name for the function. Second set of lines is used to define input parameters Eg: (Str char(10), StrLen int) defines the parameter Str as a 10 characters and defines the parameter StrLen as an integer
UDF cont’d Third line declares the one variable returned eg: RETURNS CHAR(10) Means return a 10 character value Fourth Line declares the language of the function: Language SQL
UDF Cont’d Fifth line: Returns null on null input Means that if there are no input values to the function, then NULL will be returned. Sixth line: BEGIN Starts the code section of the function
UDF Cont’d Last line END Comments start with --
Coding functions DECLARE Used to declare variables SET used to assign values to variables RETURN used to return the output parameter
Coding Functions Most built-in SQL functions can be used including case Case statements must end in end case Each statement must end in an ‘;’
Build a function that calculates a Letter Grade based on a Final Mark
Coding UDF create function dbpract.gradeStr (grade dec(5,2)) returns char(2) language sql returns null on null input begin -- declare variables declare chargrade char(2); -- calculate char grade case when grade >= 90 then set chargrade = 'A+'; when grade >= 80 then set chargrade = 'A'; else set chargrade = 'F'; end case; return chargrade; end