Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 22

RDBMS Using Oracle

Lecture week 4

Lecture Overview
■ LPAD, RPAD
■ LTRIM, RTRIM, TRIM
■ SUBSTR
■ REPLACE
■ TRNSLATE ROUND, CEIL
■ SOUNDA FLOOR, ABS
SIN, SINH
■ SINGLE ROW COS, COSH
TAN, TANH
NUMARIC FUNCTIONS 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 LPAD(ENAME)
---------- ----------
SMITH *****SMITH
ALLEN *****ALLEN
WARD ******WARD
JONES *****JONES
MARTIN ****MARTIN
BLAKE *****BLAKE

SQL> select ename, RPAD(ename ,10 ,


‘*‘ ) from emp;

ENAME LPAD(ENAME)
---------- ----------
SMITH SMITH
***** ALLEN
ALLEN ***** WARD
WARD ****** JONES
JONES *****
MARTIN 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 LTRIM(JOB)
--------- ---------
CLERK CLERK
SALESMA ALESMAN
N ALESMAN
SALESMA MANAGER
N ALESMAN
MANAGER
MANAGER
SALESMA
‘S’ is removed from the beginning of JOB column
N
MANAGER
RTRIM

SQL> select job , RTRIM ( JOB , ‘MAN‘ ) from


emp;
JOB RTRIM(JOB)
--------- ---------
CLERK CLERK
SALESMA SALES
N SALES
SALESMA MANAGER
N SALES
MANAGER MANAGER

SALESMA
‘MAN’ is removed from the right side of JOB column
N
MANAGER

Removing more then one character

SQL> select JOB, LTRIM(JOB, 'CS') from


emp;
JOB LTRIM(JOB
--------- ---------
CLERK LERK
SALESMA ALESMAN
N ALESMAN
SALESMA MANAGER
N ALESMAN
MANAGER MANAGER

SALESMA
N
MANAGER
Used of LPAD and LTRIM
SQL> select JOB, LPAD ( LTRIM (JOB, ‘CS‘ ) , 10 ,
'*')
from emp;
JOB LPAD(LTRIM
--------- ----------
CLERK ******LERK
SALESMA ***ALESMAN
This is
N ***ALESMAN
workin
SALESMA ***MANAGE
g like
N R
this
MANAGER ***ALESMAN
***MANAGE
LPAD
SALESMA ( LTRIM
R (JOB, ‘CS‘ ) , 10 ,
N
'*')
MANAGER

TRIM
■ TRIM ([[<C1>]<C2> from] <C3>) can take three
arguments where C2 and C3 are character strings.

SELECT TRIM(TRAILING 'n' FROM cust_name)


FROM customer;

■ 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 FULLY PADED ')
trim(' test1, LEFT PADED') test2,
trim('('Right Paded
trim ')test3
from dual;

TEST1 TEST2 TEST3


--------- ---------- ---------- FULLY
PADED LEFT PADED Right Paded

Replace
■ REPLACE Takes Three
Arguments REPLACE (<C1>,
<C2>[,<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
SALESMAN. Output € JOB
------
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 SU
--------- --
SALESMA SA
Note:- SUBSTRB is similar to SUBSTR but
N SA
here Last two arguments they are counted in
SALESMA MA
integers Instead of characters.
N SA
MANAGER
M
SALESMA A
N
MANAGER

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’,’*’)


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
SQL> select soundex('DAWES') from
----
SOUN
dual;
S53 ----
0 D200
A450
SQL> select soundex('DAYS')from
W63 SOUN
dual;
----
0
D200
J520
M63
Try for some other SQL> select soundex('DAWS') from dual;
5
similar word SOUN
B420 ----
??????????????? D200
????
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


C3 are ignored.

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

TRANSL
------
65432
1SQL>select ename, translate(ename, 'ABCDEF', '123456') from emp

ENAME TRANSLATE(
---------- ----------
SMITH SMITH
ALLEN 1LL5N NOTE:-
WARD W1R4 Practice and Analyze the
JONES JON5S difference in REPLACE AND
MARTIN M1RTIN TRANSLATE
Function by applying
BLAKE 2L1K5
CLARK 3L1RK both functions on same data
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, LOG & others
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 SAL/22
---------- ----------
SMITH 36.3636364
ALLEN 72.7272727
WARD 56.8181818
JONES 20.2840909
MARTIN 56.8181818
BLAKE 19.4318182
ROUND Function
■ Use of ROUND Function
SQL> select ename, sal/22 , ROUND (sal/22)
from emp;

ENAME SAL/22 ROUND(SAL/22)


---------- ---------- -------------
SMITH 36.3636364 36
ALLEN 72.7272727 73
WARD 56.8181818 57
JONES 20.2840909 20
MARTIN 56.8181818 57
BLAKE 19.4318182 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 SAL/22 ROUND(SAL/22 , 1) ROUND(SAL/22 , 2)


---------- ---------- --------------- ---------------
SMITH 36.3636364 36.4 36.36
ALLEN 72.7272727 72.7 72.73
WARD 56.8181818 56.8 56.82
JONES 20.2840909 20.3 20.28
MARTIN 56.8181818 56.8 56.82
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 COMM SAL COMM-SAL


---------- ---------- ---------- ----------
ALLEN 300 1600 -1300
WARD 500 1250 -750
MARTIN 1400 1250 150
TURNER 0 1500 -1500
GEO 800 1500 -700
■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 COMM SAL ABS(COMM-SAL)


---------- ---------- ---------- ----------
ALLEN 300 1600 1300
WARD 500 1250 750
MARTIN 1400 1250 150
TURNER 0 1500 1500
GEO 800 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
CEIL Function Example
SQL> select sal , sal / 22 , CEIL (sal/22) from emp;

SAL SAL/22 CEIL(SAL/22)


---------- ---------- ------------
800 36.3636364 37
1600 72.7272727 73
1250 56.8181818 57
446.25 20.2840909 21
1250 56.8181818 57
427.5 19.4318182 20
367.5 16.7045455 17

FLOOR Function Example


SQL> select sal , sal / 22 , FLOOR (sal/22) from emp;

SAL SAL/22 FLOOR(SAL/22)


---------- ---------- ------------
800 36.3636364 36
1600 72.7272727 72
1250 56.8181818 56
446.25 20.2840909 20
1250 56.8181818 56
427.5 19.4318182 19
367.5 16.7045455 16
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 o
emp; r
■ Select sin (sal) , cos (sal) from o
emp; r
■ SQL> select cos (45) from dual;
etc….. etc
Use of
EXP(mathematical function)
&
LN & LOG
(Logarithm Function)

LN function is used to find Natural log

SQL> select sal, LN(sal) from emp;


SQL> select LN(45) from dual;
SAL LN(SAL)
---------- ---------- LN(45)
800 6.6846117 ----------
1600 3 3.8066624
1250 7.3777589 9
446.2 1 SQL>
5 7.1308988 select
1250 3 EXP(24)
6.1008793 from dual;
3
EXP(24)
7.1308988 ----------
3 2.6489E+1
List and Description of All Numeric Functions

List and Description of All Numeric Functions contd…


List and Description of All Numeric Functions contd…

Thanks

You might also like