Function
A function is a block of code that returns a single value. This block of code is known by its name. Users can create their own function using oracle pl-sql code.
Built-in/Sql Function
Built-in functions definition is created by oracle itself. you can directly use built-in function in select query to retrieve information.
User Define Function
User can also create their own function using pl-sql code.
Single Row Function
Single row function operate on single rows and return one result per row.
Multiple Row Function
Multiple row functions also called aggregate function manipulate group of rows to give one result per group of rows.
Single Row Function
1. Character Function
Character Functions
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Syntax
|
Descriptions
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
LOWER(string)
|
Convert every letter
in a string to lower case.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
UPPER(string)
|
Convert
every letter in a string to upper case.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
INITCAP(string)
|
Capitalizes the first
letter of a word or series of words.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
CONCAT(value1,value2)
|
Concates
or combine string together.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
SUBSTR(string,start,no
of character)
|
Pick substring from string
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
LENGTH(string)
|
Length
of string
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
INSTR(string,Char,Occurance)
*Occurance
is optional
|
Finds numeric
position of named character.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
LPAD(value,length,padding_char.)
|
Makes
a string a certain length by adding a certain set of character to the left.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
RPAD(value,length,padding_char.)
|
Makes a string a
certain length by adding a certain set of character to the right.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
TRIM(char
from string);
|
Enables
you to trim heading or trailling characters(or both) from a character string.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
LTRIM(string,trim_char)
|
Trim
left side character.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
RTRIM(string,trim_char)
|
Trim right side character.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
REPLACE(string,Search_string, Replace_string)
|
Searches
a text expression for a character string and, if found, replaces it with a
specified replacement string.
|
Character Functions Example
|
||
Syntax
|
Example
|
Result
|
LOWER(string)
|
Select LOWER('READER
Viewer') from dual;
|
reader
viewer
|
UPPER(string)
|
Select UPPER('reader Viewer') from dual;
|
READER VIEWER
|
INITCAP(string)
|
Select
INITCAP('READER VIEWER') from dual;
|
Reader
Viewer
|
CONCAT(value1,value2)
|
Select
CONCAT(‘Reader’,’Viewer’) from dual;
|
ReaderViewer
|
SUBSTR(string,start,no
of character)
|
Select
SUBSTR(‘ReaderViewer’,1,6) from dual;
|
Reader
|
LENGTH(string)
|
Select
LENGTH(‘ReaderViewer’) from dual;
|
12
|
INSTR(string,Charcter
Value)
|
Select
INSTR(‘ReaderViewer’,’V’) from dual;
|
7
|
LPAD(value,length,padding
char.)
|
Select
LPAD(1000,10,’#’) from dual;
|
######1000
|
RPAD(value,length,padding
char.)
|
Select
RPAD(1000,10,’*’) from dual;
|
1000******
|
TRIM(char
from string);
|
Select
TRIM(‘R’ from ‘READER’) from dual;
|
EADER
|
LTRIM(string,char)
|
Select
LTRIM(‘###READER’,’#’) from dual;
|
READER
|
RTRIM(string,char)
|
Select RTRIM(‘READER###’,’#’) from dual;
|
READER
|
REPLACE(string,old char, new char)
|
Select
REPLACE(‘READER’,’R’,’ L’) from dual;
|
LEADER
|
Ex. : LOWER,UPPER and INITCAP.
Ex. LENGTH function in employee table.
Ex. RPAD ( Right Side padding)
Ex. LPAD (Left side padding)
Ex. TRIM, LTRIM and RTRIM functions
Ex. REPLACE function.
2. NUMBER functions
Number
Functions
|
|||||||||
ABS(value)
|
Absolute
value | Non negative value
|
||||||||
CEIL(value)
|
Smallest
integer larger than or equal to value
|
||||||||
FLOOR(value)
|
Largest
integer smaller than or equal to value
|
||||||||
POWER(value,exponent)
|
Value
raised to an exponent Power.
|
||||||||
ROUND(value,precision)
|
Rounding
of value to precision
|
||||||||
MOD(value,divisor)
|
Returns
remainder of division.
|
||||||||
TRUNC(value,precision)
|
value
truncated to precision
|
Ex. ABS (Absolute Value).
Ex. CEIL and FLOOR Functions
Ex. Power Function.
Ex. MOD Function.
Ex. Round Fucntion
Ex. TRUNC Function
3. DATE functions
Date Functions
|
||||
MONTHS_BETWEEN(date1,date2)
|
Months between date1 and date2
|
|||
ADD_MONTHS(date,n)
|
Add
Months to date
|
|||
NEXT_DAY(date,’day’)
|
Gives date of next day after date. Day like Monday, Tuesday etc.
|
|||
LAST_DAY(date)
|
Gives
date of last day of month that date is in.
|
|||
ROUND(date,’format’)
|
Returns date rounded to the unit specified by the format.
|
|||
TRUNC(date,’format’)
|
Returns
date with the time portion of the day truncated to the unit specified by the
format model.
|
Ex. MONTHS(BETWEEN and ADD_MONTHS functions.
Ex. NEXT_DAY and LAST_DAY Functions
Ex. ROUND and TRUNC functions
4. Conversion Functions.
Conversion Functions
|
|||
TO_CHAR(number/date,’format’)
|
Converts a number or date value to a VARCHAR2 character string with
format.
|
||
TO_NUMBER(string,’format’)
|
Convert
a character string containing digits to a number in the format.
|
||
TO_DATE(string,’format’)
|
Converts a string in a given format into an oracle date.
|
Ex. TO_CHAR function
Ex. TO_NUMBER function
Ex. TO_DATE function
5. General Functions
General Functions
|
|||
NVL(expr1, expr2)
|
Conversion of expr1 null value to expr2 value.
|
||
NVL2(expr1,expr2,expr3)
|
if
expr1 is not null then returns expr2 otherwise expr3
|
||
NULLIF(expr1,expr2)
|
Comparers two expression and returns null if they are equal otherwise
returns first expression
|
||
COALESCE(expr1,…,exprn)
|
Returns
the first non-null expression in the expression list.
|
Ex. NVL, NVL2, NULLIF, COALESCE
CASE Expression and DECODE function
Case and Decode functions provide IF-THEN-ELSE logic in SQL statement.CASE Expression
Syntax:
CASE <expr>
when <Cond1> then <resultant value or calculation1>
when <Cond2> then <resultant value or calculation2>
.....
.....
Else <else_expr>
END
EX.
DECODE Function
Syntax:
DECODE
(
<expr>,
<Cond1>,<resultant value or calculation1>,
<Cond2>,<resultant value or calculation2>,
...........,
...........,
<else_expr>
)
Ex.
No comments:
Post a Comment