functions oracle labs 5 & 6. 2/3/2005adapted from introduction to oracle: sql and pl/sql 2 sql...

28
Functions Oracle Labs 5 & 6

Upload: peregrine-martin

Post on 31-Dec-2015

225 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 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

Functions

Oracle Labs 5 & 6

Page 2: 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

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

2

SQL Functions

Function

arg n

arg 2

arg 1

.

.

.

Input

ResultingValue

Output

Page 3: 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

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

3

SQL Functions

• Single-Row Functions

• Multiple-Row Functions

Page 4: 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

2/3/2005 Adapted 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

Page 5: 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

2/3/2005 Adapted 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

Page 6: 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

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

6

Single-Row Function Types

• Character

• Number

• Date

• Conversion

• General

Page 7: 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

2/3/2005 Adapted 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

Characterfunctions

Page 8: 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

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

8

Character FunctionsFunction Purpose

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 letterOf 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 atCharacter 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 FROMtrim_source)

Enables you to trim heading or trailing characters (or both) fromA character string. If trim_character or trim_source is a character literal, you

must enclose it in single quotes.

Page 9: 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

2/3/2005 Adapted 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,…])Function_name (column|expression, [arg1, arg2,…])

Page 10: 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

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

10

Other Single Row Functions

• Number functions

• Date functions

• Format functions

• Conversion functions

Page 11: 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

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

11

NVL FunctionConverts Null to a Value

• Syntax NVL (expr1, expr2)

• expr1 – Source value or expression – that may contain null

• expr2 value to replace null

Page 12: 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

2/3/2005 Adapted 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’)

Page 13: 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

2/3/2005 Adapted 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

Page 14: 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

Multiple-Row Functions

Or Group Functions

Page 15: 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

2/3/2005 Adapted 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

Page 16: 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

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

16

Types of Group Functions

• AVG

• COUNT

• MAX

• MIN

• STDDEV

• SUM

• VARIANCE

Page 17: 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

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

17

SELECT [column,] groupfunction(column)FROM table[WHERE condition][GROUP BY group_by_expression][HAVING group_condition][ORDER BY column]

Page 18: 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

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

18

Functions for Only Numeric Data

• AVG

• SUM

• VARIANCE

• STDDEV

• MAX & MIN used for any datatype

Page 19: 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

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

19

Examples

Page 20: 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

2/3/2005 Adapted 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

Page 21: 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

2/3/2005 Adapted 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

Page 22: 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

2/3/2005 Adapted 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

Page 23: 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

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

23

Function Description

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

Page 24: 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

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

24

DISTINCT Option

• Consideration of only non-duplicate values

• Default – ALL

• Example

Page 25: 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

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

25

Groups Within Groups

• Multiple columns in Group By clause

• Top-level group listed first

• Example

Page 26: 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

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

26

ProblemDisplay the deptno and average salary

of all departments that have an average salary greater than 2000.

Page 27: 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

2/3/2005 Adapted 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

Page 28: 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

2/3/2005 Adapted 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