Presentation is loading. Please wait.

Presentation is loading. Please wait.

Functions Oracle Labs 5 & 6. 2/3/2005Adapted from Introduction to Oracle: SQL and PL/SQL 2 SQL Functions Function arg n arg 2 arg 1. Input Resulting Value.

Similar presentations


Presentation on theme: "Functions Oracle Labs 5 & 6. 2/3/2005Adapted from Introduction to Oracle: SQL and PL/SQL 2 SQL Functions Function arg n arg 2 arg 1. Input Resulting Value."— Presentation transcript:

1 Functions Oracle Labs 5 & 6

2 2/3/2005Adapted from Introduction to Oracle: SQL and PL/SQL 2 SQL Functions Function arg n arg 2 arg 1. Input Resulting Value Output

3 2/3/2005Adapted from Introduction to Oracle: SQL and PL/SQL 3 SQL Functions Single-Row Functions Multiple-Row Functions

4 2/3/2005Adapted from Introduction to Oracle: SQL and PL/SQL 4 Single-Row Functions Acts on each row Can accept multiple arguments –Column name –Variable name –Expression –Constant Returns a single value for each row

5 2/3/2005Adapted from Introduction to Oracle: SQL and PL/SQL 5 Single-Row Function Use Select Where Order By Any where a column name can be used

6 2/3/2005Adapted from Introduction to Oracle: SQL and PL/SQL 6 Single-Row Function Types Character Number Date Conversion General

7 2/3/2005Adapted from Introduction to Oracle: SQL and PL/SQL 7 Character Functions LOWER UPPER INITCAP CONCAT SUBSTR LENGTH INSTR LPAD RPAD TRIM REPLACE Case conversion functions Character manipulation functions Character functions

8 2/3/2005Adapted from Introduction to Oracle: SQL and PL/SQL 8 Character Functions FunctionPurpose LOWER(column\expression)Converts alpha character values to lowercase UPPER(column\expression)Converts alpha character values to uppercase INITCAP(column\expression)Converts alpha character values to uppercase for the first letter Of each word, all other letters in lowercase CONCAT(column\expression\expression1, Column2\expression2) Concatenates the first character value to the second character value; equivalent to concatenation operator(||) SUBSTR(column\expressio,m[,n])Returns specified characters from character value starting at Character position m, n characters long (If m is negative, the count starts from the end of the character value. If n is omitted, all characters to the end of the string are returned.) LENGTH(column\expression)Returns the number of characters in value INSTR(column\expression, m)Returns the numeric position of a named character LPAD(column\expression, n, ‘string’) Pads the character value right-justified to a total width of N character positions TRIM(leading\trailing\both, trim_character FROM trim_source) Enables you to trim heading or trailing characters (or both) from A character string. If trim_character or trim_source is a character literal, you must enclose it in single quotes.

9 2/3/2005Adapted from Introduction to Oracle: SQL and PL/SQL 9 Character Functions Oracle Examples Accepts character Input Returns a single value Value can be –Character –Numeric Function_name (column|expression, [arg1, arg2,…])

10 2/3/2005Adapted from Introduction to Oracle: SQL and PL/SQL 10 Other Single Row Functions Number functions Date functions Format functions Conversion functions

11 2/3/2005Adapted from Introduction to Oracle: SQL and PL/SQL 11 NVL Function Converts Null to a Value Syntax  NVL (expr1, expr2) expr1 –Source value or expression –that may contain null expr2 value to replace null

12 2/3/2005Adapted from Introduction to Oracle: SQL and PL/SQL 12 NVL Function Datatypes could be – Date –Character –Number Datatypes must match. –NVL(comm, 0) –NVL(hiredate, ’01-Jan-97’) –NVL(job, ‘No Job Yet’)

13 2/3/2005Adapted from Introduction to Oracle: SQL and PL/SQL 13 Good Things to Know Whole number too large –For format model –Replaced by string of pound signs (#) Decimal value too large –For format model –Replaced by rounded value

14 Multiple-Row Functions Or Group Functions

15 2/3/2005Adapted from Introduction to Oracle: SQL and PL/SQL 15 Multiple Rows  Single Result ENAME SAL ---------- ---------- SMITH 800 ALLEN 1600 WARD 1250 JONES 2975 MARTIN 1250 BLAKE 2850 CLARK 2450 SCOTT 3000 KING 5000 TURNER 1500 ADAMS 1100 JAMES 950 FORD 3000 MILLER 1300 MAX(SAL) ---------- 5000

16 2/3/2005Adapted from Introduction to Oracle: SQL and PL/SQL 16 Types of Group Functions AVG COUNT MAX MIN STDDEV SUM VARIANCE

17 2/3/2005Adapted from Introduction to Oracle: SQL and PL/SQL 17 SELECT[column,] groupfunction(column) FROMtable [WHEREcondition] [GROUP BY group_by_expression] [HAVING group_condition] [ORDER BYcolumn]

18 2/3/2005Adapted from Introduction to Oracle: SQL and PL/SQL 18 Functions for Only Numeric Data AVG SUM VARIANCE STDDEV MAX & MIN used for any datatype

19 2/3/2005Adapted from Introduction to Oracle: SQL and PL/SQL 19 Examples

20 2/3/2005Adapted from Introduction to Oracle: SQL and PL/SQL 20 The Count Function COUNT(*)  –Number of rows in the table –Including Duplicate rows Null Values Example

21 2/3/2005Adapted from Introduction to Oracle: SQL and PL/SQL 21 The Count Function COUNT(expr)  –Number of nonnull rows in The column Identified by expr Example

22 2/3/2005Adapted from Introduction to Oracle: SQL and PL/SQL 22 Group Functions & Null Values Null values ignored in calculations Use NVL function to ‘work-around’ Examples

23 2/3/2005Adapted from Introduction to Oracle: SQL and PL/SQL 23 FunctionDescription AVG(|DISTINCT|ALL|n) Average value of n, ignoring null values COUNT({*|DISTINCT|ALL|expr}) Number of rows, where expr evaluates to something other than null. (* counts duplicates and nulls) MAX(|DISTINCT|ALL|expr) Maximum value ignoring null values MIN(|DISTINCT|ALL|expr) Minimum value ignoring null values STDDEV(|DISTINCT|ALL|n) Standard deviation of n, ignoring null values Sum(|DISTINCT|ALL|n) Sum of n, ignoring null values VARIANCE(|DISTINCT|ALL|n) Variance of n, ignoring null values

24 2/3/2005Adapted from Introduction to Oracle: SQL and PL/SQL 24 DISTINCT Option Consideration of only non-duplicate values Default – ALL Example

25 2/3/2005Adapted from Introduction to Oracle: SQL and PL/SQL 25 Groups Within Groups Multiple columns in Group By clause Top-level group listed first Example

26 2/3/2005Adapted from Introduction to Oracle: SQL and PL/SQL 26 Problem Display the deptno and average salary of all departments that have an average salary greater than 2000.

27 2/3/2005Adapted from Introduction to Oracle: SQL and PL/SQL 27 HAVING Clause Restricts –groups of rows –Based on group conditions Like Where clause –Filters –Based on single-row conditions After GROUP BY Before ORDER BY

28 2/3/2005Adapted from Introduction to Oracle: SQL and PL/SQL 28 Having Clause Oracle Server Steps 1.Group rows. 2.Apply group function. 3.Filter by matching criteria in HAVING clause EXAMPLE PROBLEM


Download ppt "Functions Oracle Labs 5 & 6. 2/3/2005Adapted from Introduction to Oracle: SQL and PL/SQL 2 SQL Functions Function arg n arg 2 arg 1. Input Resulting Value."

Similar presentations


Ads by Google