Function and types

27
Function What is a Function in PL/SQL? A function are named PL/SQL Block.It is similar to a procedure. The major difference between a procedure and a function is, a function must always return a value, but a procedure may or may not return a value.

Transcript of Function and types

Page 1: Function  and types

Function

What is a Function in PL/SQL?

A function are named PL/SQL Block.It is similar to a procedure. The major difference between a procedure and a function is, a function must always return a value, but a procedure may or may not return a value.

Page 2: Function  and types

Structure of Function

stored functions 3 sections1.declaration section-declaration of variables and constants2.executable section-pl/sql statements which perform specific task3.exception handling section-the error occuring in executable part can be handled in this section

Page 3: Function  and types

SYNTAX

CREATE [OR REPLACE] FUNCTION function_name [ (parameter [,parameter]) ] RETURN return_datatype IS | AS [declaration_section] BEGIN executable_section [EXCEPTION exception_section] END [function_name];

Page 4: Function  and types

Example

CREATE [OR REPLACE] FUNCTION fa(m number)RETURN numberIS f number:=1;BEGIN for I in 1….mLoopF:=f*I;End loop; return f;End;

Page 5: Function  and types

Two Types Functions

Single-row functions

Multiple-row functions

Return one result per row

Return one result per set of rows

Functions

Page 6: Function  and types

Single row functions return a single result per every row. There are different types of single-row functions.

Single-Row Functions

Page 7: Function  and types

Conversion

Character

Number

Date

General Single-row functions

Single-Row Functions Types

Page 8: Function  and types

Characterfunctions

LOWERUPPERINITCAP

CONCATSUBSTRLENGTHINSTRLPAD | RPADTRIMREPLACE

Case-manipulationfunctions

1) Character or Text Functions: Character functions accept character input and can return both character and number values.

Character-manipulationfunctions

Page 9: Function  and types

1.LOWER The Lower function converts the character values into

lowercase letters. 2. UPPER The Upper function converts the character values into

uppercase letters. 3. INITCAP The Initcap function coverts the first character of each

word into uppercase and the remaining characters into lowercase.

Case-manipulation functions

.

.

Page 10: Function  and types

Character-manipulation functions

1.CONCAT

The Concat function coverts the first string with the second string

2. SUBSTR The Substr function returns specified characters from character value starting at position m and n characters long. If you omit n, all characters starting from position m to the end are returned.

Page 11: Function  and types

3.LENGTH The Length function is used to find the number of characters in a string

4.RPAD The Rpad function pads the character value left-justified to a total width of n character positions.

5.TRIM The Trim function removes the leading or trailing or both the characters from a string.

Page 12: Function  and types

6.REPLACE The Replace function is used to replace a character with another Character in a string.

Page 13: Function  and types

2) Numeric Functions: Numeric functions accept numeric input and return numeric values.

ROUNDTRUNC

Numeric functions

Page 14: Function  and types

3) Date Functions:

Date functions operate on DATE.

Date function

MONTHS_BETWEEN ADD_MONTHS NEXT_DAY LAST_DAY

Page 15: Function  and types

Explicit data type conversion

Data type conversion

Implicit data type conversion

4) Conversion Functions:

Conversion functions convert a value from one datatype to another.

Page 16: Function  and types

EXAMPLE

sql>create table stu1( name varchar2(8), regno varchar2(10) mark number(6,2), DOB date );Table created.

SQL> desc stu1; Name Null? Type ----------------------------------------- -------- -------------- NAME VARCHAR2(8) REGNO VARCHAR2(10) DOB DATE Mark NUMBER

Page 17: Function  and types

SQL> insert into stuu2 values(&name,&regno,&dob,&mark);Enter value for name: 'xx'Enter value for regno: '13sms01'Enter value for dob: '27-apr-2013'Enter value for mark: 78.23old 1: insert into stuu2 values(&name,&regno,&dob,&mark)new 1: insert into stuu2 values('xx','13sms01','27-apr-2013',78.23)

1 row created.

Page 18: Function  and types

SQL> /Enter value for name: 'yy'Enter value for regno: '13sms02'Enter value for dob: '24-jan-2012'Enter value for mark: 89.78old 1: insert into stuu2 values(&name,&regno,&dob,&mark)new 1: insert into stuu2 values('yy','13sms02','24-jan- 2012',89.78)

1 row created.

Page 19: Function  and types

SQL> select*from stu1;

NAME REGNO DATE MARK-------- ---------- ----------------- ------------------xx 13sms01 27-apr-2013 78.23yy 13sms02 24-jan-2012 89.78

CASE-MANIPULATION FUNCTION

SQL> select 2 lower(name),upper(name),initcap(name) from stu1;

LOWER(NA UPPER(NA INITCAP(-------- -------- --------xx XX Xxyy YY Yy

Page 20: Function  and types

CHARACTER MANIPULATION FUNCTION

SQL> select concat(name,regno), substr(regno,3,2), length(name), rpad(regno,10,'*')RPAD, lpad(regno,10,'*')LPAD, replace(regno,‘sms','ma')"replace”from stu1;;

CONCAT(NAME,REGNO) SUB LENGTH(NAME) RPAD ------------------ -- ------------ ---------- ---------- --------------------xx13sms01 sm 2 13sms01*** yy13sms02 sm 2 13sms02***

LPAD replace ---------- ---------- ***13sms01 13sms01 ***13sms02 13sms02

Page 21: Function  and types

NUMERIC FUNCTIONS

SQL> select round(mark,1), trunc(mark,1) from stu1;

ROUND(MARK,1) TRUNC(MARK,1) ------------- -------------

78.2 78 89.3 89

Page 22: Function  and types

DATE FUNCTION

SQL> select add_months(dob,2), last_day(dob) from stu1;

ADD_MONTH LAST_DAY(--------- ---------27-JUN-13 30-APR-1320-MAR-12 31-JAN-1227-JUN-13 30-APR-1324-MAR-12 31-JAN-12

Page 23: Function  and types

Multiple-Row Functions

Functions that take a collection of values as input and return a single value. These functions are known as group functions and aggregate function.

Types: 1.avg - Returns avg value of a given expression 2.Min - Returns Minimum value of a given expression 3.Max - Returns Maximum value of a given expression. 4.Sum - Returns total or sum of the expr. 5.count-Counts the no of values present in a column.

Page 24: Function  and types

EXAMPLE

sql>create table stu2( major varchar2(10), semester varchar2(10) noofpapers number(2), );Table created.

SQL> desc stu2; Name Null? Type ----------------------------------------- -------- -------------- MAJOR VARCHAR2(10) SEMESTER VARCHAR2(10) NOOFPAPERS NUMBER

Page 25: Function  and types

SQL> insert into stu2 values(&major,&semester,&noofpapers);Enter value for name: ‘computer'Enter value for regno: ‘first’Enter value for dob: 6old 1: insert into stuu2 values(&major,&semester,&noofpapers)new 1: insert into stuu2 values(‘computer’,’first’,6)

1 row created.

SQL> insert into stu2 values(&major,&semester,&noofpapers);Enter value for name: ‘computer'Enter value for regno: ‘second’Enter value for dob: 5old 1: insert into stuu2 values(&major,&semester,&noofpapers)new 1: insert into stuu2 values(‘computer’,’second’,5)

1 row created

Page 26: Function  and types

SQL> insert into stu2 values(&major,&semester,&noofpapers);Enter value for name: ‘history'Enter value for regno: ‘first’Enter value for dob: 6old 1: insert into stuu2 values(&major,&semester,&noofpapers)new 1: insert into stuu2 values(‘history’,’first’,6)

1 row created

Page 27: Function  and types

MULTIPLE ROW FUNCTION

Sql>select major,sum(noofpapers), count(noofpapers), avg(noofpapers) from stu2 group by major;

MAJOR SUM(NOOFPAPERS) COUNT(NOOFPAPERS) AVG(NO-------- -------- -------- -----------computer 11 2 5.5history 6 2 3