You are on page 1of 22

RDBMS Using Oracle

Lecture week 4

Lecture Overview
LPAD, RPAD
LTRIM, RTRIM, TRIM
SUBSTR
REPLACE
TRNSLATE
SOUNDAX
SINGLE ROW
NUMARIC FUNCTIONS

ROUND, CEIL
FLOOR, ABS
SIN, SINH
COS, COSH
TAN, TANH
EXP. LN, LOG & others

LPAD and RPAD

LPAD and RPAD both are used to pad(stuffing)


characters.

LAPD is used to pad extra characters at the


beginning (left side) of column values.

Similarly RAPD is used to pad extra characters


at the right side (end) of column values.

LPAD and RPAD take three arguments.

LPAD (<C1>, <i>[,<C2>]) RPAD (<C1>, <i>[,<C2>])


The

first is the character string (column) to be


operated on.

The second argument is the number of


characters to pad with it.

and

the optional third argument is the character


to pad it with (default is Single Space).

SQL> select ename, LPAD(ename ,10 , * )


from emp;
ENAME
---------SMITH
ALLEN
WARD
JONES
MARTIN
BLAKE

LPAD(ENAME)
---------*****SMITH
*****ALLEN
******WARD
*****JONES
****MARTIN
*****BLAKE

SQL> select ename, RPAD(ename ,10 , * )


from emp;
ENAME
---------SMITH
ALLEN
WARD
JONES
MARTIN

LPAD(ENAME)
---------SMITH *****
ALLEN *****
WARD ******
JONES *****
MARTIN ****

Benefit Right Align

Similarly we can use a table entries


for making table of contents etc..

LTRIM and RTRIM

LTRIM and RTRIM are used to REMOVE any


characters from the values of any specified column.

LTRIM and RTRIM takes two arguments


The first is the character string (column) to be
operated on.
and the optional 2nd argument are the characters
that are to be eliminated. (default is Single Space).

LTRIM
SQL> select job , LTRIM ( JOB , S ) from emp;
JOB
--------CLERK
SALESMAN
SALESMAN
MANAGER
SALESMAN
MANAGER

LTRIM(JOB)
--------CLERK
ALESMAN
ALESMAN
MANAGER
ALESMAN
MANAGER

S is removed from the beginning of JOB column

RTRIM
SQL> select job , RTRIM ( JOB , MAN ) from emp;
JOB
--------CLERK
SALESMAN
SALESMAN
MANAGER
SALESMAN
MANAGER

RTRIM(JOB)
--------CLERK
SALES
SALES
MANAGER
SALES
MANAGER

MAN is removed from the right side of JOB column

Removing more then one character


SQL> select JOB, LTRIM(JOB, 'CS') from emp;
JOB
--------CLERK
SALESMAN
SALESMAN
MANAGER
SALESMAN
MANAGER

LTRIM(JOB
--------LERK
ALESMAN
ALESMAN
MANAGER
ALESMAN
MANAGER

Used of LPAD and LTRIM


SQL> select JOB, LPAD ( LTRIM (JOB, CS ) , 10 , '*')

from emp;
JOB
--------CLERK
SALESMAN
SALESMAN
MANAGER
SALESMAN
MANAGER

LPAD(LTRIM
---------******LERK
***ALESMAN
***ALESMAN
***MANAGER
***ALESMAN
***MANAGER

This is
working
like this

LPAD ( LTRIM (JOB, CS ) , 10 , '*')

TRIM

TRIM ([[<C1>]<C2> from] <C3>) can take three


arguments where C2 and C3 are character strings.
If present, C1 can be following literals:
LEADING, TRAILING OR BOTH. This function returns
C3 with all (leading, trailing or both) occurrence of
characters in C2 removed.
A NULL is returned if any one of C1, C2 or C3 is NULL.
C1 is default to BOTH, C2 defaults to a space character

SQL> select trim(' FULLY PADED ') test1,


trim(' LEFT PADED') test2,
trim ('Right Paded ')test3 from dual;
TEST1
--------FULLY PADED

TEST2
---------LEFT PADED

TEST3
---------Right Paded

Replace

REPLACE Takes Three Arguments


REPLACE (<C1>, <i>[,<C3>])

Where C1, C2 and C3 are character Strings

This function returns C1 with all occurrences of C2 replace


with C3.

C3 Defaults to NULL, all occurrences of C2 are removed

If C2 is NULL then C1 returns unchanged

This function is useful for dynamic substitutions

Example
Select REPLACE (UPTOWN, UP,
DOWN) from dual;
Output

DOWNTOWN

Practice (Replace)

Apply REPLACE on JOB column of EMP table


and Replace SALES to NULL for JOB
Output JOB
SALESMAN.
-----MAN
MAN

SQL> select replace(ename, 'AL', '?=') from emp


REPLACE(ENAME,'AL','
-------------------SMITH
?=LEN
WARD

SUBSTR
SUBSTR Function is used to get any part of a String.
For Example.
SUBSTR( JOB , 1 , 2)
will Returns two characters of JOB starting from first
character.

SQL> select job , substr(job, 1, 2) from emp;


JOB
--------SALESMAN
SALESMAN
MANAGER
SALESMAN
MANAGER

SU
-SA
SA
MA
SA
MA

Note:- SUBSTRB is similar to SUBSTR but here


Last two arguments they are counted in integers
Instead of characters.

Can U do it?

Apply SUBSTR on JOB column and fetch last


character of each job. If this last character is N
then replace it with *

REPLACE(SUBSTR(job, -1), N,*)

10

SOUNDEX

SOUNDEX (<c1>) takes a single argument of


Character String.

This function returns the Soundex Photonic


representation if C1.

The SOUNDEX function is usually used to find


name that sound alike.

SQL> select soundex(ename) from emp;


SOUN
---S530
A450
W630
J520
M635
B420

SQL> select soundex('DAWES') from dual;


SOUN
---D200
SQL> select soundex('DAYS')from dual;
SOUN
---D200

Try for some other SQL> select soundex('DAWS') from dual;


SOUN
similar word
---??????????????????? D200

11

TRANSLATE

TRANSLATE (<C1>,<C2>,<C3>) takes three character string


arguments.

The function returns C1 with all occurrences of characters in C2


replaced with the positionally corresponding characters in C3.

A NULL is returned if any one of C1, C2 or C3 is NULL.

If C3 has fewer characters then C2, then unmatched character in C2 are


removed from C1.

If C2 has fewer characters then C3, then unmatched character from


from C3
are ignored.

select translate('ABCDEF', 'FEDCBA', '123456') from dual


TRANSL
-----654321
SQL>select ename, translate(ename, 'ABCDEF', '123456') from emp
ENAME
---------SMITH
ALLEN
WARD
JONES
MARTIN
BLAKE
CLARK

TRANSLATE(
---------SMITH
1LL5N
W1R4
JON5S
M1RTIN
2L1K5
3L1RK

NOTE:Practice and Analyze the difference


in REPLACE AND TRANSLATE
Function by applying
both functions on same data

12

Single ROW Numeric


Functions
Numeric Functions operate on numeric data
and perform some kind of mathematical or
arithmetic manipulation

Single ROW Numeric


Functions

There is a huge list of Single ROW Numeric Functions,


few of them are listed below.

ROUND
CEIL
FLOOR
ABS
SIN, SINH
COS, COSH
TAN, TANH
EXP. LN, LOG & others

13

ROUND Function
The round function rounds a number to a
Specified number of decimal places.
Example (Next slide)

ROUND Function

Suppose we have written following SQL


statement
SQL> select ename, sal/22 from emp;
ENAME
---------SMITH
ALLEN
WARD
JONES
MARTIN
BLAKE

SAL/22
---------36.3636364
72.7272727
56.8181818
20.2840909
56.8181818
19.4318182

14

ROUND Function

Use of ROUND Function

SQL> select ename, sal/22 , ROUND (sal/22)

from emp;
ENAME
---------SMITH
ALLEN
WARD
JONES
MARTIN
BLAKE

SAL/22
---------36.3636364
72.7272727
56.8181818
20.2840909
56.8181818
19.4318182

ROUND(SAL/22)
------------36
73
57
20
57
19

Use of ROUND Function to round result to


one, two, three or more decimal places

SQL> select ename, sal/22 , ROUND (sal/22 , 1) ,

ROUND (sal/22 , 2) from emp;


ENAME
---------SMITH
ALLEN
WARD
JONES
MARTIN

SAL/22
---------36.3636364
72.7272727
56.8181818
20.2840909
56.8181818

ROUND(SAL/22 , 1)
--------------36.4
72.7
56.8
20.3
56.8

ROUND(SAL/22 , 2)
--------------36.36
72.73
56.82
20.28
56.82

15

ABS (Absolute)
Function
The ABS function returns the
positive value of the number.
For example

In the following SQL statement COMM SAL


will return ve values (where the value of
COMM is less then the SAL value)

SQL> select ename, comm, sal, comm sal

from emp where comm is not null


ENAME
---------ALLEN
WARD
MARTIN
TURNER
GEO

COMM
---------300
500
1400
0
800

SAL
---------1600
1250
1250
1500
1500

COMM-SAL
----------1300
-750
150
-1500
-700

16

The

ABS function ABS (COMM SAL)

returns the positive value of the number.


SQL> select ename, comm, sal, ABS(comm sal)
from emp where comm is not null
ENAME
---------ALLEN
WARD
MARTIN
TURNER
GEO

COMM
---------300
500
1400
0
800

SAL
---------1600
1250
1250
1500
1500

ABS(COMM-SAL)

---------1300
750
150
1500
700

CEIL & FLOOR


Function
CEIL & FLOOR both are used to convert numbers
with decimals places into integers.
CEIL is used to get higher integer value
e.g 2.34 or 2.7 or 2.1 or 2.8 (will be converted into) 3
FLOOR is used to get lower integer value
e.g 2.34 or 2.7 or 2.1 or 2.8 (will be converted into) 2

17

CEIL Function Example


SQL> select sal , sal / 22 , CEIL (sal/22) from emp;
SAL
---------800
1600
1250
446.25
1250
427.5
367.5

SAL/22
---------36.3636364
72.7272727
56.8181818
20.2840909
56.8181818
19.4318182
16.7045455

CEIL(SAL/22)
-----------37
73
57
21
57
20
17

FLOOR Function Example


SQL> select sal , sal / 22 , FLOOR (sal/22) from emp;
SAL
---------800
1600
1250
446.25
1250
427.5
367.5

SAL/22
---------36.3636364
72.7272727
56.8181818
20.2840909
56.8181818
19.4318182
16.7045455

FLOOR(SAL/22)
-----------36
72
56
20
56
19
16

18

USE of
SIN, SINH, COS, COSH,
TAN, TANH
Functions

USE of SIN, SINH, COS,


COSH,TAN, TANH

Various trignometic computations can be carried out by


using SIN, SINH, COS, COSH, TAN and TANH.

For example
SQL> select sal , sin (sal) from emp;
Select sin (sal) , cos (sal) from emp;
SQL> select cos (45) from dual;

or
or

etc.. etc

19

Use of
EXP(mathematical function)
&
LN & LOG
(Logarithm Function)

LN function is used to find Natural log


SQL> select sal, LN(sal) from emp;
SAL
---------800
1600
1250
446.25
1250

LN(SAL)
---------6.68461173
7.37775891
7.13089883
6.10087933
7.13089883

SQL> select LN(45) from dual;


LN(45)
---------3.80666249
SQL> select EXP(24) from dual;
EXP(24)
---------2.6489E+10

20

List and Description of All Numeric Functions

List and Description of All Numeric Functions contd

21

List and Description of All Numeric Functions contd

Thanks

22

You might also like