Upload
peregrine-martin
View
225
Download
0
Embed Size (px)
Citation preview
Functions
Oracle Labs 5 & 6
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
2/3/2005 Adapted from Introduction to Oracle: SQL and PL/SQL
3
SQL Functions
• Single-Row Functions
• Multiple-Row Functions
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
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
2/3/2005 Adapted from Introduction to Oracle: SQL and PL/SQL
6
Single-Row Function Types
• Character
• Number
• Date
• Conversion
• General
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
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.
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,…])
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
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
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’)
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
Multiple-Row Functions
Or Group Functions
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
2/3/2005 Adapted from Introduction to Oracle: SQL and PL/SQL
16
Types of Group Functions
• AVG
• COUNT
• MAX
• MIN
• STDDEV
• SUM
• VARIANCE
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]
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
2/3/2005 Adapted from Introduction to Oracle: SQL and PL/SQL
19
Examples
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
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
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
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
2/3/2005 Adapted from Introduction to Oracle: SQL and PL/SQL
24
DISTINCT Option
• Consideration of only non-duplicate values
• Default – ALL
• Example
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
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.
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
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