sql lecture-4
TRANSCRIPT
-
8/10/2019 SQL Lecture-4
1/22
RDBMS Using OracleRDBMS Using Oracle
Lecture week 4Lecture week 4
Lecture OverviewLecture Overview
LPAD, RPADLPAD, RPAD
LTRIM, RTRIM, TRIMLTRIM, RTRIM, TRIM
SUBSTRSUBSTR
REPLACEREPLACE TRNSLATETRNSLATE
SOUNDAXSOUNDAX
SINGLE ROWSINGLE ROW
NUMARIC FUNCTIONSNUMARIC FUNCTIONS
ROUND, CEILROUND, CEIL
FLOOR, ABSFLOOR, ABS
SIN, SINHSIN, SINH
COS, COSHCOS, COSH
TAN, TANHTAN, TANH
EXP. LN, LOG & othersEXP. LN, LOG & others
-
8/10/2019 SQL Lecture-4
2/22
LPADLPAD andand RPADRPAD
LPAD and RPAD both are used to pad(stuffing)LPAD and RPAD both are used to pad(stuffing)characters.characters.
LAPDLAPD is used to pad extra characters at theis used to pad extra characters at the
beginning (left side) of column values.beginning (left side) of column values.
SimilarlySimilarly RAPDRAPD is used to pad extra charactersis used to pad extra charactersat the right side (end) of column values.at the right side (end) of column values.
LPAD and RPAD take three arguments.LPAD and RPAD take three arguments.LPAD (, [,])LPAD (, [,]) RPAD (, [,])RPAD (, [,])
The first is the character string (column) to beThe first is the character string (column) to beoperated on.operated on.
The second argument is the number ofThe second argument is the number ofcharacters to pad with it.characters to pad with it.
and the optional third argument is the characterand the optional third argument is the characterto pad it with (default is Single Space).to pad it with (default is Single Space).
-
8/10/2019 SQL Lecture-4
3/22
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 ****
-
8/10/2019 SQL Lecture-4
4/22
-
8/10/2019 SQL Lecture-4
5/22
LTRIM and RTRIMLTRIM and RTRIM
LTRIM and RTRIMLTRIM and RTRIM are used to REMOVE anyare used to REMOVE any
characters from the values of any specified column.characters from the values of any specified column.
LTRIM and RTRIM takesLTRIM and RTRIM takes twotwo argumentsarguments
The first is the character string (column) to beThe first is the character string (column) to be
operated on.operated on.
and the optional 2nd argument are the charactersand the optional 2nd argument are the characters
that are to be eliminated. (default is Single Space).that are to be eliminated. (default is Single Space).
SQL> select job , LTRIM ( JOB , S ) from emp;
JOB LTRIM(JOB)
--------- ---------
CLERK CLERK
SALESMAN ALESMANSALESMAN ALESMAN
MANAGER MANAGER
SALESMAN ALESMAN
MANAGER MANAGER
S is removed from the beginning of JOB column
LTRIM
-
8/10/2019 SQL Lecture-4
6/22
SQL> select job , RTRIM ( JOB , MAN ) from emp;
JOB RTRIM(JOB)
--------- ---------
CLERK CLERK
SALESMAN SALES
SALESMAN SALES
MANAGER MANAGER
SALESMAN SALES
MANAGER MANAGER
MAN is removed from the right side of JOB column
RTRIM
Removing more then one characterRemoving more then one character
SQL> select JOB, LTRIM(JOB, 'CS') from emp;
JOB LTRIM(JOB
--------- ---------CLERK LERK
SALESMAN ALESMAN
SALESMAN ALESMAN
MANAGER MANAGER
SALESMAN ALESMAN
MANAGER MANAGER
-
8/10/2019 SQL Lecture-4
7/22
Used of LPAD and LTRIMUsed of LPAD and LTRIMSQL> select JOB, LPAD ( LTRIM (JOB, CS ) , 10 , '*')
from emp;
JOB LPAD(LTRIM
--------- ----------
CLERK ******LERK
SALESMAN ***ALESMAN
SALESMAN ***ALESMAN
MANAGER ***MANAGER
SALESMAN ***ALESMAN
MANAGER ***MANAGER
LPAD ( LTRIM (JOB, CS ) , 10 , '*')
This is
working
like this
TRIMTRIM TRIM ([[] from] ) can take threeTRIM ([[] from] ) can take three
arguments where C2 and C3 are character strings.arguments where C2 and C3 are character strings.
If present, C1 can be following literals:If present, C1 can be following literals:
LEADING, TRAILING OR BOTH. This function returnsLEADING, TRAILING OR BOTH. This function returns
C3 with all (leading, trailing or both) occurrence ofC3 with all (leading, trailing or both) occurrence ofcharacters in C2 removed.characters in C2 removed.
A NULL is returned if any one of C1, C2 or C3 is NULL.A NULL is returned if any one of C1, C2 or C3 is NULL.
C1 is default to BOTH, C2 defaults to a space characterC1 is default to BOTH, C2 defaults to a space character
-
8/10/2019 SQL Lecture-4
8/22
SQL> select trim(' FULLY PADED ') test1,SQL> select trim(' FULLY PADED ') test1,trim(' LEFT PADED') test2,trim(' LEFT PADED') test2,
trim ('Righttrim ('Right PadedPaded ')test3 from dual;')test3 from dual;
TEST1TEST1 TEST2TEST2 TEST3TEST3
------------------ -------------------- --------------------
FULLY PADEDFULLY PADED LEFT PADEDLEFT PADED RightRight PadedPaded
ReplaceReplace
REPLACE Takes Three ArgumentsREPLACE Takes Three Arguments
REPLACE (, [,])REPLACE (, [,])
Where C1, C2 and C3 are character StringsWhere C1, C2 and C3 are character Strings
This function returns C1 with all occurrences of C2 replaceThis function returns C1 with all occurrences of C2 replacewith C3.with C3.
C3 Defaults to NULL, all occurrences of C2 are removedC3 Defaults to NULL, all occurrences of C2 are removed
If C2 is NULL then C1 returns unchangedIf C2 is NULL then C1 returns unchanged
-
8/10/2019 SQL Lecture-4
9/22
This function is useful for dynamic substitutionsThis function is useful for dynamic substitutions
ExampleExample
Select REPLACE (UPTOWN, UP,Select REPLACE (UPTOWN, UP,
DOWN) from dual;DOWN) from dual;
Output DOWNTOWNOutput DOWNTOWN
Practice (Replace)Practice (Replace)
Apply REPLACE on JOB column of EMP tableApply REPLACE on JOB column of EMP table
and Replace SALES to NULL for JOBand Replace SALES to NULL for JOB
SALESMAN.SALESMAN.
SQL> select replace(ename, 'AL', '?=') from emp
REPLACE(ENAME,'AL','
--------------------
SMITH
?=LEN
WARD
OutputOutputJOBJOB
------------
MANMAN
MANMAN
-
8/10/2019 SQL Lecture-4
10/22
-
8/10/2019 SQL Lecture-4
11/22
SOUNDEXSOUNDEX
SOUNDEX () takes a single argument ofSOUNDEX () takes a single argument ofCharacter String.Character String.
This function returns theThis function returns the SoundexSoundex PhotonicPhotonic
representation if C1.representation if C1.
The SOUNDEX function is usually used to findThe SOUNDEX function is usually used to find
name that sound alike.name that sound alike.
SQL> select soundex(ename) from emp;
SOUN
----
S530
A450
W630
J520M635
B420
SQL> select soundex('DAWES') from dual;
SOUN
----
D200
SQL> select soundex('DAYS')from dual;
SOUN
----
D200
SQL> select soundex('DAWS') from dual;
SOUN
----
D200
Try for some other
similar word
???????????????????
-
8/10/2019 SQL Lecture-4
12/22
TRANSLATETRANSLATE TRANSLATE (,,) takes three character stringTRANSLATE (,,) takes three character string
arguments.arguments.
The function returns C1 with all occurrences of characters in C2The function returns C1 with all occurrences of characters in C2replaced with thereplaced with the positionallypositionallycorresponding characters in C3.corresponding characters in C3.
A NULL is returned if any one of C1, C2 or C3 is NULL.A NULL is returned if any one of C1, C2 or C3 is NULL.
If C3 has fewer characters then C2, then unmatched character inIf C3 has fewer characters then C2, then unmatched character in C2 areC2 areremoved from C1.removed from C1.
If C2 has fewer characters then C3, then unmatched character froIf C2 has fewer characters then C3, then unmatched character from C3m C3are ignored.are ignored.
select translate('ABCDEF', 'FEDCBA', '123456') from dual
TRANSL
------
654321
SQL>select ename, translate(ename, 'ABCDEF', '123456') from emp
ENAME TRANSLATE(---------- ----------
SMITH SMITH
ALLEN 1LL5N
WARD W1R4
JONES JON5S
MARTIN M1RTIN
BLAKE 2L1K5
CLARK 3L1RK
NOTE:-
Practice and Analyze the difference
in REPLACE AND TRANSLATE
Function by applying
both functions on same data
-
8/10/2019 SQL Lecture-4
13/22
Single ROW NumericSingle ROW NumericFunctionsFunctions
Numeric Functions operate on numeric dataNumeric Functions operate on numeric dataand perform some kind of mathematical orand perform some kind of mathematical or
arithmetic manipulationarithmetic manipulation
Single ROW NumericSingle ROW Numeric
FunctionsFunctions
There is a huge list of Single ROW Numeric Functions,There is a huge list of Single ROW Numeric Functions,few of them are listed below.few of them are listed below.
ROUNDROUND
CEILCEIL FLOORFLOOR
ABSABS
SIN, SINHSIN, SINH
COS, COSHCOS, COSH
TAN, TANHTAN, TANH
EXP. LN, LOG & othersEXP. LN, LOG & others
-
8/10/2019 SQL Lecture-4
14/22
ROUND FunctionROUND Function
The round function rounds a number to a
Specified number of decimal places.
Example (Next slide)
ROUND FunctionROUND Function
Suppose we have written following SQLSuppose we have written following SQL
statementstatement
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
-
8/10/2019 SQL Lecture-4
15/22
ROUND FunctionROUND Function
Use ofUse of ROUND FunctionSQL> 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 20MARTIN 56.8181818 57
BLAKE 19.4318182 19
Use ofUse 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
-
8/10/2019 SQL Lecture-4
16/22
ABS (Absolute)ABS (Absolute)
FunctionFunction
The ABS function returns theThe ABS function returns the
positive value of the number.positive value of the number.
For exampleFor example
In the following SQL statementIn the following SQL statement COMMCOMMSALSAL
will returnwill return vevevalues (where the value ofvalues (where the value of
COMM is less then the SAL value)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
-
8/10/2019 SQL Lecture-4
17/22
The ABS functionThe ABS functionABS (COMM SAL)
returns the positive value of the numberreturns 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 150TURNER 0 1500 1500
GEO 800 1500 700
CEILCEIL && FLOORFLOOR
FunctionFunctionCEIL & 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
FLOORis used to get lower integer value
e.g 2.34 or 2.7 or 2.1 or 2.8 (will be converted into) 2
-
8/10/2019 SQL Lecture-4
18/22
-
8/10/2019 SQL Lecture-4
19/22
USE ofUSE ofSIN, SINH, COS, COSH,SIN, SINH, COS, COSH,
TAN, TANHTAN, TANH
FunctionsFunctions
USE ofUSE of SIN, SINH, COS,SIN, SINH, COS,
COSH,TAN, TANHCOSH,TAN, TANH
VariousVarious trignometictrignometic computations can be carried out bycomputations can be carried out by
usingusing SIN, SINH, COS, COSH, TAN and TANH.SIN, SINH, COS, COSH, TAN and TANH.
For exampleFor example
SQL> selectSQL> select salsal , sin (, sin (salsal) from) from empemp;; oror
Select sin (Select sin (salsal) ,) , coscos ((salsal) from) from empemp;; oror
SQL> selectSQL> select coscos (45) from dual;(45) from dual;
etc.. etcetc.. etc
-
8/10/2019 SQL Lecture-4
20/22
Use ofUse of
EXPEXP(mathematical function)(mathematical function)
&&
LN & LOGLN & LOG(Logarithm Function)(Logarithm Function)
LNLN function is used to find Natural logfunction is used to find Natural log
SQL> select sal, LN(sal) from emp;
SAL LN(SAL)
---------- ----------800 6.68461173
1600 7.37775891
1250 7.13089883
446.25 6.10087933
1250 7.13089883
SQL> select LN(45) from dual;
LN(45)----------
3.80666249
SQL> select EXP(24) from dual;
EXP(24)
----------
2.6489E+10
-
8/10/2019 SQL Lecture-4
21/22
List and Description of All Numeric Functions
List and Description of All Numeric Functions contd
-
8/10/2019 SQL Lecture-4
22/22
List and Description of All Numeric Functions contd
ThanksThanks