Lecture06_IDB (1)
Lecture06_IDB (1)
Input Output
Function
arg 1 Function
performs action
arg 2
Result
value
arg n
Two Types of SQL Functions
Functions
Single-row Multiple-row
functions 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 datatype
•Can be nested
function_name
function_name (column|expression,
(column|expression, [arg1,
[arg1, arg2,...])
arg2,...])
Single-Row Functions
Character
General Number
Single-row
functions
Conversion Date
Character Functions
Character
functions
Function Result
LOWER('SQL Course') sql course
UPPER('SQL Course') SQL COURSE
INITCAP('SQL Course') Sql Course
Using Case Conversion Functions
Display the employee number, name, and department number for employee
Blake.
SQL>
SQL> SELECT
SELECT empno,
empno, ename,
ename, deptno
deptno
22 FROM
FROM emp
emp
33 WHERE
WHERE ename
ename == 'blake';
'blake';
no
no rows
rows selected
selected
EMPNO
EMPNO ENAME
ENAME DEPTNO
DEPTNO
---------
--------- ---------- ---------
---------- ---------
7698
7698 BLAKE
BLAKE 30
30
Character Manipulation Functions
Function Result
CONCAT('Good', 'String') GoodString
SUBSTR('String',1,3) Str
LENGTH('String') 6
INSTR('String', 'r') 3
Trim(‘S’ from ‘SSMITH’) MITH
Replace(‘toy’,’y’,’let’) tolet
Using the Character Manipulation
Functions
•Oracle stores dates in an internal numeric format: century, year, month, day, hours,
minutes, seconds.
•The default date format is DD-MON-YY.
•SYSDATE is a function returning date.
•CURRENT_TIMESTAMP is a function returning date and time.
•DUAL is a dummy table used to view SYSDATE, CURRENT_TIMESTAMP and other things.
Arithmetic with Dates
ENAME WEEKS
---------- ---------
KING 830.93709
CLARK 853.93709
MILLER 821.36566
Date Functions
Function Description
• MONTHS_BETWEEN ('01-SEP-95','11-JAN-94')
19.6774194
• LAST_DAY('01-SEP-95') '30-SEP-95'
Using Date Functions
Datatype
conversion
From To
VARCHAR2 or CHAR NUMBER
NUMBER VARCHAR2
DATE VARCHAR2
Implicit Datatype Conversion
For expression evaluation, the Oracle Server can automatically convert the
following:
From To
VARCHAR2 or CHAR NUMBER
TO_NUMBER TO_DATE
TO_CHAR TO_CHAR
TO_CHAR Function with Dates
TO_CHAR(date,
TO_CHAR(date, 'fmt')
'fmt')
The format model:
•Must be enclosed in single quotation marks and is case
sensitive
•Can include any valid date format element
•Has an fm element to remove padded blanks or suppress
leading zeros
•Is separated from the date value by a comma
Elements of Date Format Model
HH24:MI:SS AM 15:45:32 PM
Add character strings by enclosing them in double quotation marks.
ENAME HIREDATE
---------- -----------------
KING 17 November 1981
BLAKE 1 May 1981
CLARK 9 June 1981
JONES 2 April 1981
MARTIN 28 September 1981
ALLEN 20 February 1981
...
14 rows selected.
TO_CHAR Function with Numbers
TO_CHAR(number,
TO_CHAR(number, 'fmt')
'fmt')
Use these formats with the TO_CHAR function to display a number value as a
9
character:
Represents a number
0 Forces a zero to be displayed
$ Places a floating dollar sign
L Uses the floating local currency symbol
. Prints a decimal point
, Prints a thousand indicator
Using TO_CHAR Function with
Numbers
SALARY
--------
$3,000
TO_NUMBER and TO_DATE Functions
TO_NUMBER(char[,
TO_NUMBER(char[, 'fmt'])
'fmt'])
TO_DATE(char[,
TO_DATE(char[, 'fmt'])
'fmt'])
NVL Function
NVL = Null Value Logic
DECODE(col/expression,
DECODE(col/expression, search1,
search1, result1
result1
[,
[, search2,
search2, result2,...,]
result2,...,]
[,
[, default])
default])
Using the DECODE Function
SELECT
SELECTJOB,
JOB,SAL,
SAL,
DECODE(JOB,
DECODE(JOB,'ANALYST',
'ANALYST', SAL*1.1,
SAL*1.1,
‘CLERK',
‘CLERK', SAL*1.15,
SAL*1.15,
'MANAGER',
'MANAGER',SAL*1.20,
SAL*1.20,
'PRESIDENT',
'PRESIDENT',SAL)
SAL)"REVISED_SALARY"
"REVISED_SALARY"
FROM
FROM EMP
EMP
WHERE
WHEREJOB
JOBIN
IN('ANALYST',
('ANALYST','CLERK',
'CLERK','MANAGER',
'MANAGER','PRESIDENT')
'PRESIDENT')
Nesting Functions
F3(F2(F1(col,arg1),arg2),arg3)
Step 1 = Result 1
Step 2 = Result 2
Step 3 = Result 3
Nesting Functions
ENAME NVL(TO_CHAR(MGR),'NOMANAGER')
---------- -----------------------------
KING No Manager
Books
1. https://www.db-book.com/db6/slide-dir/index.html
2. https://docs.oracle.com/en/database/oracle/oracle-database/20/sqlrf/SQL-Sta
ndards.html#GUID-BCCCFF75-D2A4-43AD-8CAF-C3C97D92AC63
3. https://www.slideshare.net/HaaMeemMohiyuddin1/data-knowledge-and-infor
mation
4. https://www.slideshare.net/tabinhasan/from-data-to-wisdom
5. https://www.slideshare.net/thinnaphat.bo/