FUNCTIONS IN PL/SQL
Presented by :-
Amit Kumar
Roll No :- 05
MCA-2
year(A)
FUNCTIONS IN PL/SQL
Functions in PL/SQL group together SQL and
PL/SQL statements that perform a task and
should return a value or values to the calling
environment.
TYPES OF PL/SQL
1) In-built Function
2) User defined Function
In-built Function
These functions are classified into two
categories
1) Single row function/ Scalar functions
2) Group function/ Aggregate functions
Single Row Function
ASCII (‘<c1>’):-This function returns the
ASCII decimal value of first character in
c1.Here ‘c1’ is a character string
SQL>select ASCII(‘A’) from dual;
e.g:-
LPAD
LPAD(‘<C1>’ , ’<I>’ , ‘<C2>’) :- This
function returns the character string c1
expanded in length to ‘I’ characters
using c2 to fill the blank spaces on the
left hand side of c1.
SQL>select lpad(‘accounting’,12,’*’)
from dual;
LTRIM
LTRIM(‘<C1>’,’<C2>’) :- This function will
return the c1 without any leading character
appeared in c2. if no c2 character are leading
character in c1 then c1 is returned unchanged.
SQL> select LTRIM(‘NISHA’,’N’) from
dual;
REPLACE
REPLACE(‘<c1>’ , ‘<c2>’ , ‘<c3>’) :- This
function return c1 with all occurrences of
c2 replaced with c3. If c3 is null, all
occurrences of c2 are removed.
SQL> select replace(‘hello sir’, ’ sir’ ,
’madam’) from dual;
SUBSTR
SUBSTR(‘<c1>’,’<i>’,’<j>’) :- This
function return the portion of c1 that is ‘j’
characters long & beginning at position i.
SQL> select substr(‘secure’,3,4) from
dual;
TO_CHAR
TO_CHAR( number/date,fmt) :- This
function convert a number or date value
to a varchar character string with format
model fmt. With the help of this function,
part of the date i.e. date,month or year
can also be extracted.
SQL> select sysdate,to_char
(sysdate,’FMMon ddth, yyyy’) from dual;
Extract function
Extractfunction :- This function extracts
a value from a date or interval value.
SQL> select extract(YEAR FROM DATE
'2003-08-22‘) from dual;
GROUP FUNCTIONS
Functions that act on a set of values are called
group functions. Group functions don’t process
null values & don’t return a null value.
For example :-
SUM :- This function returns the sum of values
for the selected
column.
SQL> select sum(sal) from emp;
MAX :- This function returns the minimum value
of selected
column.
SQL>select min (sal) from emp;
USER DEFINED
FUNCTIONS
Syntax for a function is :-
CREATE [OR REPLACE] FUNCTION
function_name (parameter [,parameter])
RETURN return_datatype
IS | AS
[declaration_section]
BEGIN
executable_section
END;
1) Return Type: The header section
defines the return type of the function.
The return datatype can be any of the
oracle datatype like varchar, number etc.
2) The execution and exception section
both should return a value which is of the
datatype defined in the header section.
Function creating
Function calling
Example:
Function creating
Function calling
Example:
http://www.techonthenet.com/ora
cle/index.php