0% found this document useful (0 votes)
4 views

Oracle_3

Uploaded by

mohammed.shohel9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Oracle_3

Uploaded by

mohammed.shohel9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

FUNCTIONs IN ORACLE

Functions are used to meet the business requirement.


There are two types of functions.

1) Single row functions


2) Multi row functions (or) Aggregate functions

1) Single Row Functions :

It works or executes from each of value. The following are the single row functions.

A) Number Functions
B) String Functions
C) Date Functions
D) Conversion Functions
E) General Functions.

A) NUMBER FUNCTIONS:

i)Power: power(m,n):
it returns the m raised to the n’th power .It multiply the m specified in n number of times.
EX: select power(3,3)from dual;
o/p ……>27

ii)sqrt :
it returns the squre root of specified value.
EX: select sqrt(25) from dual;

iii) mod (M,N):-


it returns remainder of m/n. It returns m if n is zero.It returns error if n is not specified.
EX:- select mod(10,3) from dual;
o/p >1

iv)ABS (M):
it returns the absolute value of m
EX:- select abs(-25) from dual:
o/p→ 25 >it always displays the positive value.

v)trunc:-
trunc function returns number truncated to certain number of decimal places.
The function returns n1 truncated to n2 decimal places.

If n2 is omitted, then n1 truncated to ‘0’ places. n2 can be negative to truncate (make zero) n2 digits left
to the decimal points.
Ex: trunc (125.815)
O/P: 125
Trunc (125.815,0)
O/P : 125
Trunc (125.815,1)
O/P : 125.8
Trunc (125.815,2)
O/P : 125.81
Trunc (-125.815,2)
O/P: -125.81
Trunc (125.815,-1)
O/P : 120
Trunc (125.815,-2)
O/P : 100

vi) Round:
The round function returns a number rounded to a certain number of decimal places.
If parameter is omitted the function will round number to ‘0’ decimal places.

Ex: select 15.999,round (15.9999),round (15.9999,2),round (15.5555,2),round (15.4899,2) from dual;


O/P : 15.999,16,15.56,15.49

vii) Sign:
It returns the sign of specified number.
If number is less than zero output is ‘-1’
If number is zero then output is ‘0’
If number is more than zero output is ‘1’

Ex: select sign (-0.1),sign (0.1),sign (0) from dual;


O/P : -1,1,0

viii) Ceil :
It returns the smallest integer greater than or equal to m
The adjustment is done to the highest nearest decimal place.
Ex: select ceil (14.27) from dual;
O/P : 15

iX) Floor:
It returns the largest integer less than or equals to m, The adjustment should done to the lowest nearest
value.
Ex: select floor (14.27) from dual;
O/P : 14
Select floor (14.99) from dual;
O/P : 14
X) ASCII :
This function returns the number code for the specified number.
Ex: select ASCII (‘t’) from dual;
O/P: 116

Select ASCII (‘T’) from dual;


O/P: 84

Xi) CHR :
It returns the character based on number code.
Ex: select chr (116) from dual;
O/P: ‘t’

Select chr (84) from dual;


O/P: ‘T’

Xii) Greatest : (exp1,exp2,…..,expn)


Greatest returns maximum value among given expressions.
Ex: select greatest (3,5,8,9) from dual;
O/P: 9

Xiii) Least: (exp1,exp2,…..,expn)


Least returns minimum value among given expressions.
Ex: select least (3,2,1,9) from dual;
O/P: 1

B. String functions:

i) Length:
Length function gives the length of character.
Ex: select length (‘Alexander’) from dual;
O/P: 7

ii) Reverse:
It is used to reverse the given string
Ex: select reverse (‘Alexnader’) from dual;

iii. Upper:
It converts the given string to upper letters.

Iv: Lower:
It converts the given string to lower letters.

V. Initcap:
It converts the given string initial letter to upper letter and remaining characters to lower letter.
Vi. Concat:
It is used to join the two strings.
We can only join two strings.

Ex: select concat (‘greg’, ‘martin’) from dual;


O/P: gregmartin

We have to use ‘||’ pipe symbol to join multiple strings.

Ex: select ‘EBS’ || ‘solutions’ || ‘for’ || ‘technical’ from dual;


O/P: EBS solutions for technical

Vii. Substr :
Substr function returns specified characters from character value starting from position m to n
characters.
Syn: substr( expression,m,n)

It returns the substring of string from ‘m’ ending to ‘n’


If m is zero it is treated as ‘1’
If m is positive oracle counts the beginning of string to find the first character.
If m is negative oracle counts back words from the end of the string.
If n is omitted the oracle counts returns all characters from Mth to end of the string

If n is < 1 or ‘0’ null is returned

Ex: select substr( technologies ) from dual;


O/P: error

Select substr (‘technologies ‘ ,2,4) from dual;


O/P: echn

Select substr (‘technolofies’,2) from dual;


O/P: echnologies

Select substr (‘technologies’,-2) from dual;


O/P: es

Select substr (‘technologies’-4,2) from dual;


O/P: gi

Select substr (‘technologies’-4,-2) from dual;


O/P: null

Select substr (‘technologies’4,0) from dual;


O/P: null
Viii. Instr:

It returns the numeric position of the a named character.


Instr function searches the numeric position of string that is mention

It searches for expression beginning with mth character for nth occurrence and returns the numeric
position of substring.
m can be positive or negative.If it is negative searches in backword direction from end of the expression.
n always be positive.

Default value of m and n are ‘1’.


If search is unsuccessful (if given pattern not found) output is zero.

Examples:
Select instr (‘orporatororders’ ‘or’) from dual;
O/P: 2

Select instr (‘orporatororders’ ‘or’,4) from dual;


O/P: 5

Select instr (‘orporatororders’ ‘or’,7) from dual;


O/P: 10

Select instr (‘orporatororders’ ‘or’,-1) from dual;


O/P: 17 [It started searching from back word direction and found or at 17th position]

Select instr (‘orporatororders’ ‘or’,1,2) from dual;


O/P: 5 [It started searching from 1st character for the 2nd occurrence of or string find at 5th position]

iX) Trim:
It trims the leading or trailing or both characters from string.
If we specify it trim only the leading characters.
If we specify trailing it trim only the trailing characters.
If we don’t specify leading /trailing/both, the trim function will remove characters from both front and
end of string
Syntax: trim (leading | heading |both, ’character’ from ‘string’);

Trim (‘0’ from 002200);


O/P: 22
Trim (leading ‘0’ from 002200);
O/P: 2200
Trim (trailing ‘0’ from 002200);
O/P: 0022
Trim (both ‘0’ from 002200);
O/P: 22
X)Ltrim:
It removes the specified characters from left side of the specified string

Ex: ltrim (‘xyz bellandurxyz’,’xyz’)


O/P: Bellandur xyz

Ltrim (‘0001230’,’0’)
O/P: 1230

Ltrim (‘6372tech1234’,’0123456789’)
O/P: tech1234

In the above example every number combination from 0-9 has been listed in the string parameter.
By doing this it does not only for order that appears the string, all leading number will be removed.

Xi. Rtrim:
It removes the specified characters from right of specified string.

Ex: ltrim (‘xyz bellandurxyz’,’xyz’)


O/P: xyz bellandur

Ltrim (‘0012300’,’0’)
O/P: 00123

Ltrim (‘6372tech1234’,’0123456789’)
O/P: 6372tech

Xii. Lpad:

The Lpad function pads (adds) the specified characters to the left side of the string.

Syn: Lpad (string, padded _length, ‘padding characters)

String à the string to pad characters

Padded_lengthà the numbers of characters to return

Padà this is the characters that will be characters padded to the left side of the string.

If we don’t specify any characters to be pad by default it will pad the spaces.

Ex: Lpad (‘ tech’,7);


O/P: ‘ tech’
Lpad (‘tech’,8,’0’);
O/P: 0000tech
Lpad (‘tech on the net’,15,’2’);
O/P:
Xiii. Rpad:

The Rpad function pads (adds) the specified characters to right of the specified string

Syn: Rpad (string, padded_length, padding characters);

Ex: Rpad (‘tech’, 7);


O/P: tech

Rpad (‘tech’, 8, ‘0’);


O/P: ‘tech0000’

Rpad (‘tech on the net’, 16, ‘z’)


O/P: ‘tech on the netz’

Xiv. Translate:

It is used to translate characters by character in a string.


The function replaces characters of a string with another set of characters.
However it replaces character by character.

Syn: Translate ( string-name, characters_to_replace, replacement characters)

Ex: translate (‘1tech23’, ‘123’, ‘456’)


O/P: ‘4tech456

Translate (‘222tech’, ‘2ec’, ‘3it)


O/P: 333tith

XV. Replace:

This function replaces a sequence of characters in a string with another set of characters.
Syntax: replace (string-name, ‘string to replace’, ‘and replacement string’)

Note: Here replacement string is optional

If we don’t specify replacement string , the replace function simply remove all occurrences of string and
give us the result output

Ex: replace (‘222tech’, ‘222’, ‘333’)


O/P: 333tech2

Replace (‘123tech324’,’123’,’456’)
O/P: 456tech324

Replace (‘123123tech’, ‘123’)


O/P: Tech

You might also like