day1 session2
Post on 02-Apr-2018
224 views
Embed Size (px)
TRANSCRIPT
7/27/2019 day1 session2
1/57
Entry Level Technology Program
Satyam Learning Center ORACLE 1 of 1
FUNCTIONS
7/27/2019 day1 session2
2/57
Entry Level Technology Program
Satyam Learning Center ORACLE 2 of 1
SQL Functions
FunctionInput
arg 1
arg 2
arg n
Functionperforms action
Output
Resultvalue
7/27/2019 day1 session2
3/57
Entry Level Technology Program
Satyam Learning Center ORACLE 3 of 1
Two Types of SQL Functions
Functions
Single-row
functions
Multiple-row
functions
7/27/2019 day1 session2
4/57
Entry Level Technology Program
Satyam Learning Center ORACLE 4 of 1
Aggregating DataUsing Group Functions
7/27/2019 day1 session2
5/57
Entry Level Technology Program
Satyam Learning Center ORACLE 5 of 1
What Are Group Functions?
Group functions operate on sets of rows togive one result per group.EMPLOYEES
The maximumsalary inthe EMPLOYEES
table.
7/27/2019 day1 session2
6/57
Entry Level Technology Program
Satyam Learning Center ORACLE 6 of 1
Types of Group Functions
AVG
COUNT
MAX
MIN
SUM
7/27/2019 day1 session2
7/57
Entry Level Technology Program
Satyam Learning Center ORACLE 7 of 1
SELECT [column,] group_function(column), ...FROM table
[WHERE condition][GROUP BY column][ORDER BY column];
Group Functions Syntax
7/27/2019 day1 session2
8/57
Entry Level Technology Program
Satyam Learning Center ORACLE 8 of 1
SELECT AVG(SAL), MAX(SAL),
MIN(SAL), SUM(SAL)
FROM EMP
WHERE JOB LIKE '%MAN%';
Using theAVG and SUM
Functions
You can use AVG and SUM for numeric data.
7/27/2019 day1 session2
9/57
Entry Level Technology Program
Satyam Learning Center ORACLE 9 of 1
Using theMIN ,MAX and Count
Functions
You can use MIN and MAX for any data type.
SELECT MIN(HIREDATE), MAX(HIREDATE)
FROM employees;
COUNT(*) returns the number of rows in a table.
SELECT COUNT(*)
FROM EMP
WHERE DEPTNO = 10;
7/27/2019 day1 session2
10/57
Entry Level Technology Program
Satyam Learning Center ORACLE 10 of 1
SELECT COUNT(DISTINCT DEPTNO)
FROM EMP;
Using the DISTINCT Keyword
COUNT(DISTINCT expr) returns the
number of distinct non-null values ofthe expr.
Display the number of distinctdepartment values in the EMPLOYEES
table.
7/27/2019 day1 session2
11/57
Entry Level Technology Program
Satyam Learning Center ORACLE 11 of 1
SELECT AVG(COMM)
FROM EMP;
Group Functions and NullValues
Group functions ignore null values in the column.
The NVL function forces group functions to include
null values.
SELECT AVG(NVL(COMM, 0))
FROM EMP;
7/27/2019 day1 session2
12/57
Entry Level Technology Program
Satyam Learning Center ORACLE 12 of 1
Single-Row Functions
Single row functions: Manipulate data items
Accept arguments and return one value
Act on each row returned
Return one result per row
May modify the data type
Can be nested
Accept arguments which can be a column or anexpression
function_name [(arg1, arg2,...)]
7/27/2019 day1 session2
13/57
Entry Level Technology Program
Satyam Learning Center ORACLE 13 of 1
Single-Row Functions
Conversion
Character
Number
Date
General Single-rowfunctions
7/27/2019 day1 session2
14/57
Entry Level Technology Program
Satyam Learning Center ORACLE 14 of 1
Character Functions
Characterfunctions
LOWER
UPPER
INITCAP
CONCAT
SUBSTR
LENGTH
INSTRLPAD | RPAD
TRIM
REPLACE
Case-manipulationfunctions
Character-manipulationfunctions
7/27/2019 day1 session2
15/57
Entry Level Technology Program
Satyam Learning Center ORACLE 15 of 1
Function Result
Case Manipulation Functions
These functions convert case for character strings.
LOWER('SQL Course')
UPPER('SQL Course')
INITCAP('SQL Course')
sql course
SQL COURSE
Sql Course
7/27/2019 day1 session2
16/57
Entry Level Technology Program
Satyam Learning Center ORACLE 16 of 1
Using Case ManipulationFunctions
Display the employee number, name, and departmentnumber for employee Higgins:
SELECT EMPNO, ENAME, DEPTNOFROM EMPWHERE ENAME = smith';no rows selected
SELECT EMPNO, ENAME, DEPTNO
FROM EMPWHERE LOWER(ENAME) = smith';
7/27/2019 day1 session2
17/57
Entry Level Technology Program
Satyam Learning Center ORACLE 17 of 1
CONCAT('Hello', 'World')SUBSTR('HelloWorld',1,5)
LENGTH('HelloWorld')
INSTR('HelloWorld', 'W')
LPAD(salary,10,'*')RPAD(salary, 10, '*')
TRIM('H' FROM 'HelloWorld')
HelloWorldHello
10
6
*****2400024000*****
elloWorld
Function Result
Character-Manipulation Functions
These functions manipulate character strings:
7/27/2019 day1 session2
18/57
Entry Level Technology Program
Satyam Learning Center ORACLE 18 of 1
SELECT EMPNO, CONCAT(ENAME,JOB) NAME,
JOB, LENGTH (ENAME),
INSTR(ENAME, 'a') "Contains 'a'?"
FROM EMP
WHERE SUBSTR(JOB, 6) = MAN';
Using the Character-Manipulation Functions
7/27/2019 day1 session2
19/57
Entry Level Technology Program
Satyam Learning Center ORACLE 19 of 1
Number Functions
ROUND: Rounds value to specified decimal
ROUND(45.926, 2) 45.93
TRUNC: Truncates value to specified decimal
TRUNC(45.926, 2) 45.92
MOD
: Returns remainder of division
MOD(1600, 300) 100
7/27/2019 day1 session2
20/57
Entry Level Technology Program
Satyam Learning Center ORACLE 20 of 1
SELECT ROUND(45.923,2), ROUND(45.923,0),
ROUND(45.923,-1)
FROM DUAL;
Using the ROUND Function
DUAL is a dummy table you can use to view resultsfrom functions and calculations.
7/27/2019 day1 session2
21/57
Entry Level Technology Program
Satyam Learning Center ORACLE 21 of 1
SELECT TRUNC(45.923,2), TRUNC(45.923),
TRUNC(45.923,-2)
FROM DUAL;
Using the TRUNC Function
7/27/2019 day1 session2
22/57
Entry Level Technology Program
Satyam Learning Center ORACLE 22 of 1
SELECT ENAME, SAL, MOD(SAL, 5000)
FROM EMP
WHERE JOB = 'SALESMAN';
Using theMOD Function
Calculate the remainder of a salary after it is divided by 5000for all employees whose job title is sales representative.
7/27/2019 day1 session2
23/57
Entry Level Technology Program
Satyam Learning Center ORACLE 23 of 1
Working with Dates
Oracle database stores dates in an internal numeric format:century, year, month, day, hours, minutes, seconds.
The default date display format is DD-MON-RR.
Allows you to store 21st century dates in the 20thcentury by specifying only the last two digits of the year.
Allows you to store 20th century dates in the 21stcentury in the same way.
SELECT ENAME, HIREDATE
FROM EMP
WHERE ENAME like 'G%';
0
7/27/2019 day1 session2
24/57
Entry Level Technology Program
Satyam Learning Center ORACLE 24 of 1
Working with Dates
SYSDATEis a function that returns:
Date
Time
7/27/2019 day1 session2
25/57
Entry Level Technology Program
Satyam Learning Center ORACLE 25 of 1
Arithmetic with Dates
Add or subtract a number to or from a date for a
resultant date value. Subtract two dates to find the number of days
between those dates.
Add hours to a date by dividing the number of
hours by 24.
7/27/2019 day1 session2
26/57
Entry Level Technology Program
Satyam Learning Center ORACLE 26 of 1
Using Arithmetic Operatorswith Dates
SELECT ENAME, (SYSDATE-HIREDATE)/7 AS WEEKS
FROM EMP
WHERE DEPTNO = 20;
7/27/2019 day1 session2
27/57
Entry Level Technology Program
Satyam Learning Center ORACLE 27 of 1
Date Functions
Number of monthsbetween two dates
MONTHS_BETWEEN
ADD_MONTHS
NEXT_DAY
LAST_DAY
ROUND
TRUNC
Add calendar months todate
Next day of the datespecified
Last day of the month
Round date
Truncate date
Function Description
7/27/2019 day1 session2
28/57
Entry Level Technology Program
Satyam Learning Center ORACLE 28 of 1
MONTHS_BETWEEN ('01-SEP-95','11-JAN-94')
Using Date Functions
ADD_MONTHS ('11-JAN-94',6)
NEXT_DAY ('01-SEP-95','FRIDAY')
LAST_DAY('01-FEB-95')
19.6774194
'11-JUL-94'
'08-SEP-95'
'28-FEB-95'
7/27/2019 day1 session2
29/57
Entry Level Technology Program
Satyam Learning Center ORACLE 29 of 1
ROUND(SYSDATE,'MONTH') 01-AUG-95
ROUND(SYSDATE ,'YEAR') 01-JAN-96
TRUNC(SYSDATE ,'MONTH') 01-JUL-95
TRUNC(SYSDATE ,'YEAR') 01-JAN-95
Using Date Functions
Assume SYSDATE = '25-JUL-95':
7/27/2019 day1 session2
30/57
Entry Leve