Oracle_ch10.pptx

67
Dr. Chen, Oracle Database System (Oracle) 1 PL/SQL and Chapter 10 Selected Single-Row Functions  Jason C. H. Chen, Ph.D.  Pro fessor of MIS School of Business Gonzaga University Spoane! "# $$%&' US# chen()epson.gonzaga.e*u

Transcript of Oracle_ch10.pptx

Page 1: Oracle_ch10.pptx

8/10/2019 Oracle_ch10.pptx

http://slidepdf.com/reader/full/oraclech10pptx 1/67

Dr. Chen, Oracle Database System (Oracle) 1

PL/SQL andChapter 10

Selected Single-Row Functions Jason C. H. Chen, Ph.D.

 Professor of MIS 

School of BusinessGonzaga University

Spoane! "# $$%&' US#

chen()epson.gonzaga.e*u

Page 2: Oracle_ch10.pptx

8/10/2019 Oracle_ch10.pptx

http://slidepdf.com/reader/full/oraclech10pptx 2/67

Dr. Chen, Oracle Database System (Oracle) 2

Objecties

• Use the UPPER, LOWER, and INITCAP functions to change the case

of field values and character strings• Maniulate character su!strings "ith the #U$#TR and IN#TR

functions

•  Nest functions inside other functions

• %eter&ine the length of a character string using the LEN'T( function

• Use the LPA% and RPA% functions to ad a string to a certain "idth• Use the LTRIM and RTRIM functions to re&ove secific characters

strings• #u!stitute character string values "ith the REPLACE and

TRAN#LATE functions

• Round and truncate nu&eric data using the ROUN% and TRUNCfunctions

•  Return the re&ainder onl) of a division oeration using the MO% function

• Use the A$# function to set nu&eric values as ositive

Page 3: Oracle_ch10.pptx

8/10/2019 Oracle_ch10.pptx

http://slidepdf.com/reader/full/oraclech10pptx 3/67

Dr. Chen, Oracle Database System (Oracle) 3

Objecties !continued"

• Use the POWER function to raise a nu&!er to a secified o"er •

Calculate the nu&!er of &onths !et"een t"o dates using theMONT(#*$ETWEEN function• Maniulate date data using the A%%*MONT(#, NE+T*%A,

LA#T*%A, and TO*%ATE functions•  %ifferentiate !et"een CURRENT*%ATE and ##%ATE values• E-tend attern &atching caa!ilities "ith regular e-ressions

• Identif) and correct ro!le&s associated "ith calculations involving NULL values using the N.L function

• %isla) dates and nu&!ers in a secific for&at "ith the TO*C(ARfunction

• Perfor& condition rocessing si&ilar to an I/ state&ent "ith the

%ECO%E function• Use the #OUN%E+ function to identif) character honetics• Convert string values to nu&eric "ith the TO*NUM$ER function• Use the %UAL ta!le to test functions• Learn PL/SQL # Procedure Language / SQL

Page 4: Oracle_ch10.pptx

8/10/2019 Oracle_ch10.pptx

http://slidepdf.com/reader/full/oraclech10pptx 4/67

Page 5: Oracle_ch10.pptx

8/10/2019 Oracle_ch10.pptx

http://slidepdf.com/reader/full/oraclech10pptx 5/67

Dr. Chen, Oracle Database System (Oracle) 5

Re'resh the (atabase

• 34 'o to $lac9!oard and do"nload :at least

three; data files fro& Oracle chater3< and

save under c2=oradata=chater3<= – Please note that there is a PL>#?L data file of

0Ch+, -ueries for P S-.s/l 1

• 54 Run the follo"ing scrit file

 – #tart c2=oradata=chater3<=@L%$*$uild*3<4sl

Page 6: Oracle_ch10.pptx

8/10/2019 Oracle_ch10.pptx

http://slidepdf.com/reader/full/oraclech10pptx 6/67

Dr. Chen, Oracle Database System (Oracle) 6

Manipulating CharacterStrings with PL/SQL

• To change case, use UPPER , LOWER , INITCAP• IN#TR  function searches a string for a secific

su!string and return the starting osition

 start0position 12 I3S456original0string! su7string! starting0pos! occurrence89:

 – ;< the first t=o argu>ents are re/uire*! the last t=oargu>ents are optional <; 

• #U$#TR  function e-tracts a secific nu&!er ofcharacters fro& a character string, starting at a given

 oint2

e?tracte*0string 12 SUBS456string0varia7le!

 starting0point! nu>7er0of0characters9:

Page 7: Oracle_ch10.pptx

8/10/2019 Oracle_ch10.pptx

http://slidepdf.com/reader/full/oraclech10pptx 7/67

Dr. Chen, Oracle Database System (Oracle) 7

Case Conersion Functions

• Case conversion functions alter the case of

data stored in a colu&n or character string – Used in a #ELECT clause, the) alter the

aearance of the data in the results

 – Used in a W(ERE clause, the) alter the valuefor co&arison

Page 8: Oracle_ch10.pptx

8/10/2019 Oracle_ch10.pptx

http://slidepdf.com/reader/full/oraclech10pptx 8/67

Dr. Chen, Oracle Database System (Oracle) 8

LO)*R Function

Used to convert characters to lo"ercase letters

/igure 3<B3 LOWER function in the W(ERE clause

Page 9: Oracle_ch10.pptx

8/10/2019 Oracle_ch10.pptx

http://slidepdf.com/reader/full/oraclech10pptx 9/67

Dr. Chen, Oracle Database System (Oracle) 9

LO)*R Function !cont+"

• Used to convert characters to &i-ed case

/igure 3<B5 LOWER function in the #ELECT and W(ERE clauses

Page 10: Oracle_ch10.pptx

8/10/2019 Oracle_ch10.pptx

http://slidepdf.com/reader/full/oraclech10pptx 10/67

Dr. Chen, Oracle Database System (Oracle) 10

&PP*R Function

• Used to convert characters to uercase letters

• It can !e used in the sa&e "a) as the LOWERfunction

 – To affect the disla) of characters, it is used ina #ELECT clause

 – To &odif) the case of characters for a searchcondition, it is used in a W(ERE clause

• The s)nta- for the UPPER function is UPPER:c; – Where c is the character string or field to !e

converted into uercase characters

Page 11: Oracle_ch10.pptx

8/10/2019 Oracle_ch10.pptx

http://slidepdf.com/reader/full/oraclech10pptx 11/67

Dr. Chen, Oracle Database System (Oracle) 11

&PP*R Function # ,anage user input

/igure 3<B6 Using UPPER function to &anage user inut

BB chater 3<, /igure 3<B6 4668

#ELECT firstna&e, lastna&e/ROM custo&ers

W(ERE lastna&e D UPPER:FCustal;

Page 12: Oracle_ch10.pptx

8/10/2019 Oracle_ch10.pptx

http://slidepdf.com/reader/full/oraclech10pptx 12/67

Dr. Chen, Oracle Database System (Oracle) 12

.CP Function

• Used to convert character strings to &i-ed case,

"hich each "ord !eginning "ith a caital letter4

/igure 3<B7 INITCAP function in a #ELECT clause &odifies the disla)

Page 13: Oracle_ch10.pptx

8/10/2019 Oracle_ch10.pptx

http://slidepdf.com/reader/full/oraclech10pptx 13/67

Dr. Chen, Oracle Database System (Oracle) 13

Character $anipulation Functions

• Character &aniulation functions &aniulate

data !) e-tracting su!strings, counting the

nu&!er of characters, relacing strings, etc4

Page 14: Oracle_ch10.pptx

8/10/2019 Oracle_ch10.pptx

http://slidepdf.com/reader/full/oraclech10pptx 14/67

Dr. Chen, Oracle Database System (Oracle) 14

S&SR Function

• Used to return a su!string, or ortion of a string

/igure 3<BG Co&arison of #U$#TR argu&ents

#U$#TR :c! p! l ;

Where

c1 the character

string

 p2 the !eginningcharacter osition,

l 2 the length of

the string to

return in theuer) result4

-p2 !ac9"ard 01

 osition

Page 15: Oracle_ch10.pptx

8/10/2019 Oracle_ch10.pptx

http://slidepdf.com/reader/full/oraclech10pptx 15/67

Dr. Chen, Oracle Database System (Oracle) 15

.SR Function

/igure 3<BH Co&arison of IN#TR argu&ents

 start0position 12 I3S456original0string! su7string! starting0pos! occurrence89: ;< the first t=o argu>ents are re/uire*! the last t=o argu>ents are optional <; 

2 13

)h%4

Page 16: Oracle_ch10.pptx

8/10/2019 Oracle_ch10.pptx

http://slidepdf.com/reader/full/oraclech10pptx 16/67

Dr. Chen, Oracle Database System (Oracle) 16

Procedural rogra&&ing Language Uses detailed instructions

Processes state&ents seuentiall)

Co&!ines #?L co&&ands "ith

 rocedural instructions

Used to erfor& seuential

 rocessing using an Oracle data!ase

)hat is PL/SQL4

Page 17: Oracle_ch10.pptx

8/10/2019 Oracle_ch10.pptx

http://slidepdf.com/reader/full/oraclech10pptx 17/67

Dr. Chen, Oracle Database System (Oracle) 17

)h% PL/SQL4

SQL

PL/SQL

5&Co,ponents

!reports6 'or,s6

graphics etc+"

%4$4

ORCL*!progra,,ing language

co,ponents"

Page 18: Oracle_ch10.pptx

8/10/2019 Oracle_ch10.pptx

http://slidepdf.com/reader/full/oraclech10pptx 18/67

Dr. Chen, Oracle Database System (Oracle) 18

&sing SQL Co,,ands in PL/SQL

Progra,s

es

COMMIT,

ROLL$AC,

#A.EPOINT

OrganiJe %ML

co&&ands into

logical

transactions

Transaction

control

co&&ands

es#ELECT, IN#ERT,

UP%ATE, %ELETE?uer) or change

the data in the

data!ase ta!les

%ata

Maniulation

Language

:($L;

.o

CREATE, ALTER,

%ROP, 'RANT,RE.OE

Create>change

the data!asestructure

%ate %efinition

Language :((L;

Can e &sed

in PL/SQL

*7a,ples o'

Co,,ands

PurposeCategor%

Page 19: Oracle_ch10.pptx

8/10/2019 Oracle_ch10.pptx

http://slidepdf.com/reader/full/oraclech10pptx 19/67

Page 20: Oracle_ch10.pptx

8/10/2019 Oracle_ch10.pptx

http://slidepdf.com/reader/full/oraclech10pptx 20/67

Dr. Chen, Oracle Database System (Oracle) 20

able8 PL/SQL co,,and capitali9ation st%les

s*first*na&e, facult)*idLo"ercase.aria!le na&es

student, fidLo"ercase%ata!ase o!ects

#ELECT, IN#ERTUercase#?L co&&ands

.ARC(AR5, NUM$ER UercasePredefined data t)es

COUNT, TO*%ATEUercase$uiltBin functions

$E'IN, %ECLAREUercaseReserved Words

*7a,pleCapitali9ationte, %pe

#tudent*/*Na&e, /acult)*I%Mi-ed case

Page 21: Oracle_ch10.pptx

8/10/2019 Oracle_ch10.pptx

http://slidepdf.com/reader/full/oraclech10pptx 21/67

Dr. Chen, Oracle Database System (Oracle) 21

denti'iers and :ariables8Rules 'or .a,es and Properties

• /ro& 3 to 6< characters

• Onl) alhanu&eric characters, and secial

characters : * ;• Must !egin "ith a letter and can not contain

 !lan9 saces or h)hens

And sure the) cannot !e reserved "ords:e4g4, $E'IN;

Page 22: Oracle_ch10.pptx

8/10/2019 Oracle_ch10.pptx

http://slidepdf.com/reader/full/oraclech10pptx 22/67

Dr. Chen, Oracle Database System (Oracle) 22

Variables and Data Tpes

• .aria!les – Used to store nu&!ers, character strings, dates,

and other data values

 – Avoid using 9e)"ords, ta!le na&es and colu&n

na&es as varia!le na&es – Must !e declared "ith data t)e !efore use2

varia7le0na>e *ata0type0*eclaration

e4g4, current_s_id NUMBER(6);

Page 23: Oracle_ch10.pptx

8/10/2019 Oracle_ch10.pptx

http://slidepdf.com/reader/full/oraclech10pptx 23/67

Dr. Chen, Oracle Database System (Oracle) 23

Language *le,ents8

PL/SQL (ata %pes

• #calar  – References a single value

• Co&osite

 – References a data structure• Reference

 – References a secific data!ase ite& :e4g4, TPE,ROWTPE;

• LO$ – References a large !inar) o!ect :e4g4, grahs, video;

:"e "ill focus on this t)e;

Page 24: Oracle_ch10.pptx

8/10/2019 Oracle_ch10.pptx

http://slidepdf.com/reader/full/oraclech10pptx 24/67

Dr. Chen, Oracle Database System (Oracle) 24

Scalar (ata %pes

• Reresent a single value – %ata!ase scalar data t)es2

Page 25: Oracle_ch10.pptx

8/10/2019 Oracle_ch10.pptx

http://slidepdf.com/reader/full/oraclech10pptx 25/67

Dr. Chen, Oracle Database System (Oracle) 25

Scalar (ata %pes !cont+"

• Reresent a single value – NonBdata!ase scalar data t)es2

Page 26: Oracle_ch10.pptx

8/10/2019 Oracle_ch10.pptx

http://slidepdf.com/reader/full/oraclech10pptx 26/67

Dr. Chen, Oracle Database System (Oracle) 26

ables8 PL/SQL data t%pes

c*address

custo&er4custo&er*address; <P*

cust*order*record

cust*order ; RO)<P*

Assu&es the data t)e of a

data!ase record :ro";

;RO)<P*

Assu&es the data t)e of a

data!ase field

;<P*

order*flag $OOLEANTrue>/alse values$OOLEAN

toda)s*date %ATE%ates%ATE

evaluation*su&&ar) LON'Te-t, u to 65,HG< !)tesLON'

current*rice NUM$ER:8,5;/loating, fi-edBoint, or

integer nu&!er 

 NUM$ER 

student*gender C(AR:3;/i-edBlength character

strings

C(AR 

student*na&e.ARC(AR5:6<;

.aria!leBlength characterstrings

.ARC(AR5

Sa,ple (eclaration&sage(ata %pe

Page 27: Oracle_ch10.pptx

8/10/2019 Oracle_ch10.pptx

http://slidepdf.com/reader/full/oraclech10pptx 27/67

Dr. Chen, Oracle Database System (Oracle) 27

loc= Structure

PL>#?L is a 0 !loc9structured1 language4

That ust &eans ever)thing

)ou do is done in ter&s of

 !loc9s of code4

All PL>#?L !loc9s share

the sa&e !asic structure4

-- Figure 3-1> p+12?

(*CLR* >1(EA%ER1

  varia!le declarationsQ

*5.

  !od) e-ecuta!le codeQ

*@C*PO.

  e-cetion>errorhandling codeQ

*.(>

Co&&ents2

   Not e-ecuted !) interreter 

  Enclosed !et"een >K and K>

On one line !eginning "ith BB

Page 28: Oracle_ch10.pptx

8/10/2019 Oracle_ch10.pptx

http://slidepdf.com/reader/full/oraclech10pptx 28/67

Dr. Chen, Oracle Database System (Oracle) 28

Co,,ent State,ents

There are t"o t)es of co&&ents in PL>#?L2

#ingleBline and &ultiBline4

Single Line2

- 2D 7 BB assign 7 to the varia!le -

$ulti-line8

>K Assign 7 to  the varia!le - K>

- 2D 7

Page 29: Oracle_ch10.pptx

8/10/2019 Oracle_ch10.pptx

http://slidepdf.com/reader/full/oraclech10pptx 29/67

Dr. Chen, Oracle Database System (Oracle) 29

ssign,ent State,ents

Assigns a value to a varia!le – varia7le0na>e 2D value

• .alue can !e a literal2

 – current*s*first*na&e 2D @ohn• .alue can !e another varia!le2

 – current*s*first*na&e 2D s*first*na&e

!"ecuting a PL/SQL Program in

Page 30: Oracle_ch10.pptx

8/10/2019 Oracle_ch10.pptx

http://slidepdf.com/reader/full/oraclech10pptx 30/67

Dr. Chen, Oracle Database System (Oracle) 30

!"ecuting a PL/SQL Program inSQL#Plus

• Create rogra& in te-t editor:using Notead;

• Paste into #?LKPlus"indo"

• Press Enter, t)e / then enterto e-ecute

•#ET #ER.EROUTPUT ON#IE 7<<<

• #ET #ER.EROUTPUT ON

BB PL>#?L 32 KKK "ith s)nta- error KKK

%ECLARE

  toda)s*date %ATE

$E'IN

  toda)s*date D ##%ATE

  %$M#*OUTPUT4PUT*LINE:Toda)s date is ;

%$M#*OUTPUT4PUT*LINE:toda)s*date;

EN%BB PL>#?L 5

%ECLARE

  toda)s*date %ATE

$E'IN

  toda)s*date 2D ##%ATE  %$M#*OUTPUT4PUT*LINE:Toda)SSs date is ;

%$M#*OUTPUT4PUT*LINE:toda)s*date;

EN%

Page 31: Oracle_ch10.pptx

8/10/2019 Oracle_ch10.pptx

http://slidepdf.com/reader/full/oraclech10pptx 31/67

Dr. Chen, Oracle Database System (Oracle) 31

BB PL>#?L 6a :o9 for this version;

BBPL>#?L rogra& to disla) the current date%ECLARE

  toda)s*date %ATE

$E'IN

  toda)s*date 2D ##%ATE

  %$M#*OUTPUT4PUT*LINE:Toda)s date is :toda)s*date;;

EN%

BBPL>#?L6! rogra& to disla) the current date

BB CURRENT*%ATE returns the current date and ti&e fro& the user session :fro&

BB client soft"are and it &a) !e in a different location fro& the server data!ase :4688;

%ECLARE

  toda)s*date %ATE

$E'IN

  toda)s*date 2D CURRENT*%ATE

  %$M#*OUTPUT4PUT*LINE:Toda)s date is TO*C(AR:toda)s*date;;

EN%

Page 32: Oracle_ch10.pptx

8/10/2019 Oracle_ch10.pptx

http://slidepdf.com/reader/full/oraclech10pptx 32/67

Dr. Chen, Oracle Database System (Oracle) 32

(ata %pe Conersion Functions

Function (escription *7a,ple

OA(*  Converts a character string TO*%ATE:<H>37><3S,SMM>%%>S;

  to a date

OA.&$*R   Converts a character string TO*NUM$ER:5S;

  to a nu&!er 

OACBR   Converts either a nu&!er or TO*C(AR:5;

  a date to a character string TO*C(AR:##%ATE;

if toda) is2 return Octo!erS TO*C(AR:##%ATE, MONT(S;Tuesda), return Tuesda)S TO*C(AR:##%ATE, %AS;

Octo!er 38, return 38S TO*C(AR:##%ATE, %%S;

5<33 return 3326< AMS TO*C(AR:curr*ti&e, ((2MI AMS;

Page 33: Oracle_ch10.pptx

8/10/2019 Oracle_ch10.pptx

http://slidepdf.com/reader/full/oraclech10pptx 33/67

Dr. Chen, Oracle Database System (Oracle) 33

Debugging PL/SQL Programs

• #)nta- error2 – Co&&and does not follo" the guidelines of the

 rogra&&ing language

 – 'enerates co&iler or interreter error&essages

• Logic error2 –

Progra& runs !ut results in an incorrect result – Caused !) &ista9e in rogra&

 R&.-$* error

Page 34: Oracle_ch10.pptx

8/10/2019 Oracle_ch10.pptx

http://slidepdf.com/reader/full/oraclech10pptx 34/67

Dr. Chen, Oracle Database System (Oracle) 34

BB PL>#?L8a Progra& "ith a logic error and de!ugging state&ents

%ECLARE

  curr*call*id .ARC(AR5:6<; 2D MI# 3<3

  !lan9*sace NUM$ER:5;  curr*det .ARC(AR5:6<;

  curr*nu&!er .ARC(AR5:6<;

$E'IN

  !lan9*sace 2D IN#TR:curr*call*id, ;

  curr*det 2D #U$#TR:curr*call*id, 3, :!lan9*sace B 3;;

  %$M#*OUTPUT4PUT*LINE:Call I% deart&ent is2 curr*det;

  %$M#*OUTPUT4PUT*LINE:Original string value2 curr*call*id;

  %$M#*OUTPUT4PUT*LINE:#tart osition2 !lan9*sace;

  %$M#*OUTPUT4PUT*LINE:Nu&!er of characters2

:LEN'T(:curr*call*id; B !lan9*sace;;

  currAnu,ber 8 S&SR!currAcallAid6 blan=Aspace6 

!L*.5B!currAcallAid" - blan=Aspace"">

  %$M#*OUTPUT4PUT*LINE:Course Nu&!er is2 curr*nu&!er;

EN%

Call I% deart&ent is2 MI#

Original string value2 MI# 3<3

#tart osition2 7 Nu&!er of characters2 6

Course Nu&!er is2 3<

PL>#?L rocedure successfull) co&lete

Page 35: Oracle_ch10.pptx

8/10/2019 Oracle_ch10.pptx

http://slidepdf.com/reader/full/oraclech10pptx 35/67

Dr. Chen, Oracle Database System (Oracle) 35

BB PL>#?L8! Progra& "ith a correct logic

%ECLARE

  curr*call*id .ARC(AR5:6<; 2D MI# 3<3

  !lan9*sace NUM$ER:5;  curr*det .ARC(AR5:6<;

  curr*nu&!er .ARC(AR5:6<;

$E'IN

  !lan9*sace 2D IN#TR:curr*call*id, ;

  curr*det 2D #U$#TR:curr*call*id, 3, :!lan9*sace B 3;;

  %$M#*OUTPUT4PUT*LINE:Call I% deart&ent is2 curr*det;

  %$M#*OUTPUT4PUT*LINE:Original string value2 curr*call*id;

  %$M#*OUTPUT4PUT*LINE:#tart osition2 !lan9*sace;

  %$M#*OUTPUT4PUT*LINE:Nu&!er of characters2

:LEN'T(:curr*call*id; B !lan9*sace;;

  currAnu,ber 8 S&SR!currAcallAid6 !blan=AspaceD1"6 

!L*.5B!currAcallAid" - blan=Aspace"">

  %$M#*OUTPUT4PUT*LINE:Course Nu&!er is2 curr*nu&!er;

EN%

Call I% deart&ent is2 MI#

Original string value2 MI# 3<3

#tart osition2 7

 Nu&!er of characters2 6

Course Nu&!er is2 3<3

PL>#?L rocedure successfull) co&lete

Page 36: Oracle_ch10.pptx

8/10/2019 Oracle_ch10.pptx

http://slidepdf.com/reader/full/oraclech10pptx 36/67

Dr. Chen, Oracle Database System (Oracle) 36

er,inolog%

• /unction V redefined !loc9 of code that

accets argu&ents

• #ingleBro" function V returns one ro" of

results for each record rocessed

• MultileBro" function V returns one result

 er grou of data rocessed :covered in the

ne-t chater;

Page 37: Oracle_ch10.pptx

8/10/2019 Oracle_ch10.pptx

http://slidepdf.com/reader/full/oraclech10pptx 37/67

Dr. Chen, Oracle Database System (Oracle) 37

%pes o' Functions

Ta!le 3<B3 /unctions Covered in This Chater 

Page 38: Oracle_ch10.pptx

8/10/2019 Oracle_ch10.pptx

http://slidepdf.com/reader/full/oraclech10pptx 38/67

Dr. Chen, Oracle Database System (Oracle) 38

• Practice all the e-a&les in the te-t4

• A #crit file is availa!le on the $! :file

na&e2 Ch+,-ueries.s/l ;

• After co&leting all e-a&les, do the (W4

Page 39: Oracle_ch10.pptx

8/10/2019 Oracle_ch10.pptx

http://slidepdf.com/reader/full/oraclech10pptx 39/67

Dr. Chen, Oracle Database System (Oracle) 39

Bo,ewor= - Bands-On ssign,ents

Read and Practice all e-a&les on Chaters 3<• 34 Run the scrit files :in the folder

=oradata=chater3<=;2 @L%$*$uild*3<4sl anddo"nload other data files

• 54 Read Oracle assign&ent and create a scrit fileOracleAch10ALna,eAFna,e+sEl for uestions!G63610> p+GH1" on 0(andsBon Assign&ents14 Usearoriate COL&$. state&ents to roduce reada!le

oututs• 64 E-ecute and test one ro!le& at a ti&e and &a9e sure

the) are all running successfull)4• 74 When )ou done, sool the scrit files :see ne-t slide

for sooling instructions; and e&ail the file:Oracle*ch3<*#ool*Lna&e*/na&e4t-t; to &e !) the&idnight !efore the ne-t class4

E&ail &e "ith one attach&ent

:Oracle*ch3<*#ool*Lna&e*/na&e4; to2

chenIjepson+gon9aga+edu"ith su!ect title of

b,is331AOracleAch10 or ,busJ22AOracleAch10

Page 40: Oracle_ch10.pptx

8/10/2019 Oracle_ch10.pptx

http://slidepdf.com/reader/full/oraclech10pptx 40/67

Dr. Chen, Oracle Database System (Oracle) 40

Bow to Spool %our Script and Output FilesAfter )ou tested the scrit file of OracleAch10ALna,eAFna,e+sEl

successfull), follo" the instructions !elo" to sool !oth scrit and oututfiles2

#te <4 Run the follo"ing scrit file fro& #?LKPlus :since )ou have created@L%$ ta!les; – #tart c2=oradata=chater3<=@L%$*$uild*3<4sl

• 34 t)e the follo"ing on #?LQ – #ool c2=oradata=OracleAch10ASpoolALna,eAFna,e+t7t :&a9e sure )our na&e is

entered;• 54 oen Oracle*ch3<*Lna&e*/na&e4sl that )ou alread) tested

• 64 co) and aste all the #?L co&&ands :including all co&&ents; to the#?LKPLU#

• 74 t)e #ool Off on the #?LQ

The outut should contain )our ersonal infor&ation, all #?L co&&ands andtheir solution on the 4t-t file and saved in C2 drive :oradata= folder;

E&ail &e "ith the sooled file :4t-t; "ith attach&ent to2

chenIjepson+gon9aga+edu

"ith su!ect title of

b,is331AOracleAch10

Page 41: Oracle_ch10.pptx

8/10/2019 Oracle_ch10.pptx

http://slidepdf.com/reader/full/oraclech10pptx 41/67

Dr. Chen, Oracle Database System (Oracle) 41

<our urn K

• PART II – #tud) )ourself 

 – 0Learning to learn and learning to change1

Page 42: Oracle_ch10.pptx

8/10/2019 Oracle_ch10.pptx

http://slidepdf.com/reader/full/oraclech10pptx 42/67

Dr. Chen, Oracle Database System (Oracle) 42

.esting Functions

/igure 3<B IN#TR nested inside #U$#TR 

Page 43: Oracle_ch10.pptx

8/10/2019 Oracle_ch10.pptx

http://slidepdf.com/reader/full/oraclech10pptx 43/67

Dr. Chen, Oracle Database System (Oracle) 43

L*.5B Function

• Used to deter&ine the nu&!er of characters in a string

/igure 3<B3< Chec9ing data "idth "ith the LEN'T( function

Page 44: Oracle_ch10.pptx

8/10/2019 Oracle_ch10.pptx

http://slidepdf.com/reader/full/oraclech10pptx 44/67

Dr. Chen, Oracle Database System (Oracle) 44

LR$ and RR$ Functions

• Used to re&ove a secific string of characters

/igure 3<B35 Using the LTRIM function

Page 45: Oracle_ch10.pptx

8/10/2019 Oracle_ch10.pptx

http://slidepdf.com/reader/full/oraclech10pptx 45/67

Dr. Chen, Oracle Database System (Oracle) 45

R*PLC* Function

• #u!stitutes a string "ith another secified string

/igure 3<B36 Using the REPLACE function

Page 46: Oracle_ch10.pptx

8/10/2019 Oracle_ch10.pptx

http://slidepdf.com/reader/full/oraclech10pptx 46/67

Dr. Chen, Oracle Database System (Oracle) 46

R.SL* Function

/igure 3<B37 Using TRAN#LATE to su!stitute character values

Page 47: Oracle_ch10.pptx

8/10/2019 Oracle_ch10.pptx

http://slidepdf.com/reader/full/oraclech10pptx 47/67

Dr. Chen, Oracle Database System (Oracle) 47

CO.C Function

• Used to concatenate t"o character strings

/igure 3<B38 Using the CONCAT function

Page 48: Oracle_ch10.pptx

8/10/2019 Oracle_ch10.pptx

http://slidepdf.com/reader/full/oraclech10pptx 48/67

Dr. Chen, Oracle Database System (Oracle) 48

.u,ber Functions

• Allo" for &aniulation of nu&eric data – ROUN%

 – TRUNC

 – MO%

 – A$#

Page 49: Oracle_ch10.pptx

8/10/2019 Oracle_ch10.pptx

http://slidepdf.com/reader/full/oraclech10pptx 49/67

Dr. Chen, Oracle Database System (Oracle) 49

RO&.( Function

• Used to round nu&eric colu&ns to a stated recision

/igure 3<B3G Using the ROUN% function to round nu&!ers to various laces

Page 50: Oracle_ch10.pptx

8/10/2019 Oracle_ch10.pptx

http://slidepdf.com/reader/full/oraclech10pptx 50/67

Dr. Chen, Oracle Database System (Oracle) 50

R&.C Function

• Used to truncate a nu&eric value to a secific osition

/igure 3<B3H Using the TRUNC function to truncate nu&!ers at various laces

Page 51: Oracle_ch10.pptx

8/10/2019 Oracle_ch10.pptx

http://slidepdf.com/reader/full/oraclech10pptx 51/67

Dr. Chen, Oracle Database System (Oracle) 51

$O( Function

/igure 3<B3 Using the MO% functions to return the re&ainder 

S F i

Page 52: Oracle_ch10.pptx

8/10/2019 Oracle_ch10.pptx

http://slidepdf.com/reader/full/oraclech10pptx 52/67

Dr. Chen, Oracle Database System (Oracle) 52

S Function

/igure 3<B3X The effect of using the A$# function

Page 53: Oracle_ch10.pptx

8/10/2019 Oracle_ch10.pptx

http://slidepdf.com/reader/full/oraclech10pptx 53/67

Dr. Chen, Oracle Database System (Oracle) 53

(ate Functions

• Used to erfor& date calculations or for&at date values

• #u!tract date for nu&!er of da)s difference

/igure 3<B5< A calculation "ith date colu&ns

Page 54: Oracle_ch10.pptx

8/10/2019 Oracle_ch10.pptx

http://slidepdf.com/reader/full/oraclech10pptx 54/67

Dr. Chen, Oracle Database System (Oracle) 54

$O.BS A *)**. Function

• %eter&ines the nu&!er of &onths !et"een t"o

dates

/igure 3<B53 Using the MONT(#*$ETWEEN function

Page 55: Oracle_ch10.pptx

8/10/2019 Oracle_ch10.pptx

http://slidepdf.com/reader/full/oraclech10pptx 55/67

Dr. Chen, Oracle Database System (Oracle) 55

(( A $O.BS Function

• Adds a secified nu&!er of &onths to a date

/igure 3<B55 Using the A%%*MONT(# function

Page 56: Oracle_ch10.pptx

8/10/2019 Oracle_ch10.pptx

http://slidepdf.com/reader/full/oraclech10pptx 56/67

Dr. Chen, Oracle Database System (Oracle) 56

.*@ A (< Function

• %eter&ines the ne-t occurrence of a secified da)

of the "ee9 after a given date

/igure 3<B56 Using the NE+T*%A function

Page 57: Oracle_ch10.pptx

8/10/2019 Oracle_ch10.pptx

http://slidepdf.com/reader/full/oraclech10pptx 57/67

Dr. Chen, Oracle Database System (Oracle) 57

O A (* Function

• Converts various date for&ats to the internal

for&at :%%BMONB; used !) Oracle 33 g 

/igure 3<B58 Using the TO*%ATE function

F t $ d l *l t ( t

Page 58: Oracle_ch10.pptx

8/10/2019 Oracle_ch10.pptx

http://slidepdf.com/reader/full/oraclech10pptx 58/67

Dr. Chen, Oracle Database System (Oracle) 58

For,at $odel *le,ents - (ates

Ta!le 3<B5 /or&at Argu&ents for %ates

RO&.( F ti

Page 59: Oracle_ch10.pptx

8/10/2019 Oracle_ch10.pptx

http://slidepdf.com/reader/full/oraclech10pptx 59/67

Dr. Chen, Oracle Database System (Oracle) 59

RO&.( Function

/igure 3<B5G Rounding dates !) &onth and )ear 

R&.C F ti

Page 60: Oracle_ch10.pptx

8/10/2019 Oracle_ch10.pptx

http://slidepdf.com/reader/full/oraclech10pptx 60/67

Dr. Chen, Oracle Database System (Oracle) 60

R&.C Function

/igure 3<B5H Using the TRUNC function on date calculation results

Page 61: Oracle_ch10.pptx

8/10/2019 Oracle_ch10.pptx

http://slidepdf.com/reader/full/oraclech10pptx 61/67

Dr. Chen, Oracle Database System (Oracle) 61

OACBR Function

• Converts dates and nu&!ers to a for&atted

character string

/igure 3<B6X /or&atting values for disla) "ith TO*C(AR 

O .&$*R Function

Page 62: Oracle_ch10.pptx

8/10/2019 Oracle_ch10.pptx

http://slidepdf.com/reader/full/oraclech10pptx 62/67

Dr. Chen, Oracle Database System (Oracle) 62

OA.&$*R Function

/igure 3<B76 Using the TO*NUM$ER function to convert a string to a nu&eric datat)e

(&L able

Page 63: Oracle_ch10.pptx

8/10/2019 Oracle_ch10.pptx

http://slidepdf.com/reader/full/oraclech10pptx 63/67

Dr. Chen, Oracle Database System (Oracle) 63

(&L able

• %u&&) ta!le

• Consists of one colu&n and one ro"

• Can !e used for ta!le reference in the /ROM

clause

&sing (&L

Page 64: Oracle_ch10.pptx

8/10/2019 Oracle_ch10.pptx

http://slidepdf.com/reader/full/oraclech10pptx 64/67

Dr. Chen, Oracle Database System (Oracle) 64

&sing (&L

/igure 3<B77 Practicing functions !) using a %UAL ta!le

Su,,ar%

Page 65: Oracle_ch10.pptx

8/10/2019 Oracle_ch10.pptx

http://slidepdf.com/reader/full/oraclech10pptx 65/67

Dr. Chen, Oracle Database System (Oracle) 65

Su,,ar%

• #ingleBro" functions return a result for each ro" or record rocessed

• Case conversion functions such as UPPER, LOWER, andINITCAP can !e used to alter the case of character strings

• Character &aniulation functions can !e used to e-tractsu!strings :ortions of a string;, identif) the osition of a

su!string in a string, relace occurrences of a string "ith

another string, deter&ine the length of a character string, and

tri& saces or characters fro& strings•  Nesting one function "ithin another allo"s &ultileoerations to !e erfor&ed on data

Su,,ar% !continued"

Page 66: Oracle_ch10.pptx

8/10/2019 Oracle_ch10.pptx

http://slidepdf.com/reader/full/oraclech10pptx 66/67

Dr. Chen, Oracle Database System (Oracle) 66

Su,,ar% !continued"

• #i&le nu&!er functions such as ROUN% and TRUNC canround or truncate a nu&!er on !oth the left and right side of adeci&al

• The MO% function is used to return the re&ainder of a

division oeration• %ate functions can !e used to erfor& calculations "ith dates

or to change the for&at of dates entered !) a user 

• Regular e-ressions ena!le co&le- attern &atchingoerations

• The N.L, N.L5, and NULLI/ functions are used to address ro!le&s encountered "ith NULL values

Su,,ar% !continued"

Page 67: Oracle_ch10.pptx

8/10/2019 Oracle_ch10.pptx

http://slidepdf.com/reader/full/oraclech10pptx 67/67

Su,,ar% !continued"

• The TO*C(AR function lets a user resent nu&eric data anddates in a secific for&at

• The %ECO%E function allo"s an action to !e ta9en to !edeter&ined !) a secific value

• The searched CA#E e-ression ena!les )ou to evaluateconditions to deter&ine the resulting value

• The #OUN%E+ function loo9s for records !ased on the honetic reresentation of characters

•The %UAL ta!le can !e helful "hen testing functions