際際滷

際際滷Share a Scribd company logo
SQL Fundamentals Oracle 11g
M U H A M M A D WA H E E D
O R AC L E DATA BA S E D E V E LO P E R
E M A I L : m .wa h e e d 3 6 6 8 @ g m a i l . co m
Lecture#6
SQL Functions
Function Structure
2
SQL Functions(contd)
Functions are powerful feature of SQL and can be used to do the
following:
Perform calculations on data
Modify individual data items
Manipulate output for group of rows
Format dates and numbers for display
Convert column data type
3
SQL Functions(contd)
There are following two types of SQL functions:
Single Row Functions
Operate on single rows only and return single result per row.
Multiple Row Functions
Can be used to manipulate group of rows to generate single result
against group of rows and also known as Group Functions.
*For now, We will consider only single-row functions.
4
Single-Row Functions
There are following types of single-row functions:
5
Single-Row(contd)
There are following types of character function:
6
Character Functions
There are following case-manipulation functions:
LOWER(<column_name>|<expression>)
converts alpha characters to lowercase
UPPER(<column_name>|<expression>)
converts alpha characters to uppercase
INITCAP(<column_name>|<expression>)
capitalize the first letter of each word
7
Character Functions(contd)
CONCAT(<column_name1>|<expression1>,
(<column_name2>|<expression2>))
equivalent to concatination operator
SUBSTR(<column_name>|<expression>,M[,N])
returns characters starting from m and ending at n. As n is optional
if it is skipped then by default characters are returned from m to end
of characters.
LENGTH(<column_name>|<expression>)
returns total no. of characters
8
Character Functions(contd)
9
Character Functions(contd)
10
Use of Character Functions
11
Example:
SELECT std_id, CONCAT(std_first_name,std_last_name) AS
std_name, LENGTH(std_address), INSTR( std_address, #)
FROM student
WHERE SUBSTR(std_name,2,3) = ai;
Number Functions
12
Number functions input as well as output only numeric values.
Number Functions(contd)
13
Number functions input as well as output only numeric values.
Number Functions(contd)
14
It can have negative value of n.
Example:
SELECT ROUND(45.926,-1) FROM DUAL;
Result: 50
SELECT TRUNC(45.926,-2) FROM DUAL;
Result: 0
Date Functions
15
For current date we use:
SELECT SYSDATE FROM DUAL;
Result( standard/default format): 26-APR-18
Elements of Date Functions
16
Date Functions(contd)
17
Arithmetic - Date Functions(contd)
18
Use Date Functions(contd)
19
Example:
SELECT std_id, (SYSDATE - admission_date)/ 7 AS weeks
FROM student;
Use Date Functions(contd)
20
Implicit type casting
21
For assignments, Oracle server can automatically convert following:
Implicit type casting(contd)
22
For expression, Oracle server can automatically convert following:
Explicit type casting
23
Explicit type casting(contd)
24
Type casting - Date Functions(contd)
25
Example:
i- SELECT std_id, TO_CHAR(admission_date,MM/YY)
FROM student;
ii- SELECT std_id, TO_CHAR(admission_date,Day Month DD,YYYY)
AS Admission Date
FROM student;
iii- SELECT std_id, TO_CHAR(admission_date,fmDay Month DD,YYYY)
AS Admission Date
FROM student;
Type casting - Date Functions(contd)
26
Example:
i- SELECT std_id, TO_DATE(27-APR-2018,MM/YY)
FROM student;
ii- SELECT std_id, TO_DATE(MAY 27,2018,fxMonth DD,YYYY) AS
Admission Date
FROM student;
Type casting - Date Functions(contd)
27
Example:
SELECT std_id, TO_CHAR(admission_date, fmDD MONTH,YYYY
fmHH12:MI:SS AM)
FROM student;
Result(TO_CHAR): 26 APRIL,2018 2:00:00 PM
Motivational Speaking
28
NVL Function
29
Converts a null value to an actual value
Syntax:
NVL(<column/experession>,<value>)
Used with NUMBER,VARCHAR2 and DATE datatypes
Data types must match for actual value
- NVL(std_age,0)
- NVL(std_address,Pakistan)
- NVL(std_dob,01-JAN-00)
CASE Function
30
It provides IF-THEN-ELSE nesting facility in SQL
Syntax:
CASE Function(contd)
31
Example:
SELECT tch_id,tch_name,tch_salary,
CASE tch_designation WHEN VISITING salary*1.1
WHEN ASSISTANT LECTURER salary*1.2
WHEN LECTURER salary*1.5
ELSE salary END AS Revised Salary
FROM teacher;
*Note: same functionality can be performed using DECODE in
Oracle. Do it by yourself.
Records Manipulation Function
32
There are few such functions: COUNT,MAX,MIN
Example:
i- SELECT COUNT(*) FROM student;
ii- SELECT MAX(tch_salary) FROM teacher;
iii- SELECT MIN(std_dob) FROM student;
Nested Single-row Functions
33
Single functions can be nested to any level.
Nested Single-row Functions
34
Single functions can be nested to any level.
SELECT NVL(TO_CHAR(std_dob,MON DD,YYYY),NO VALUE)
FROM student;
Feedback/Suggestions?
The feedback I get is that my books are honest.
- Laurie Halse Anderson

More Related Content

SQL Functions - Oracle SQL Fundamentals