Unit 4 (SQL Functions 1)
Unit 4 (SQL Functions 1)
SQL FUNCTIONS
Objectives
At the end of this unit you will be able : Modify and p esent data in a !a iety of ways usin" a ith#eti$ and standa d SQL fun$tions%
Topics
&efe en$e Nu#e i$ Fun$tions &efe en$e 'ate Fun$tions &efe en$e Cha a$te Fun$tions &efe en$e ( oup Fun$tions
SQL FUNCTIONS
FUNCTIONS
Fun$tions a e used to #anipulate data ite#s and pe fo # $o#ple* $al$ulations on data% They a e also used to #anipulate output fo " oup of ows and $an alte date fo #ats fo display% Types of Functions Colu#n Fun$tion ( oup Fun$tion
Numeric Functions
Nu#e i$ fun$tions #anipulate nu#e i$ !alues and etu n a nu#e i$ esult% All the e*a#ples ha!e to be used with a S+L+CT state#ent Function Purpose !"mp#e $eturn %"#ue MOD(m,n) To dete #ine the #odulus of the fun$tion% &etu ns e#ainde of # di!ided by n MO',-./0 1
PO& $(m,n) To etu n the nth powe of the "i!en nu#be % &etu ns # aised to the powe of n% If n is not an inte"e it will be t un$ated% 2O3+&,-.)0 /4
SQL FUNCTIONS
C I)(n) To etu n the s#allest inte"e " eate than o e7ual to the "i!en nu#be % C+IL,-%1//0 8
F)OO$(n) To etu n the la "est inte"e e7ual to o less than the "i!en nu#be % FLOO&,-1//0 -
SI*N(n) To etu n the si"n of the "i!en nu#be %If n9:.61;if n<:. :;if n=:. 1% SI(N,/0 1
SQL FUNCTIONS
Function Purpose
S+$T(n) To etu n the s7ua e oot of the "i!en nu#be %&etu ns s7ua e oot of n; if n9 :. etu ns null% di!ided by n SQ&T,/40 -
Function Purpose
$OUND(m,n) To ound the !alue of the nu#e i$ e*p ession to the spe$ified p e$ision% >alue # is ounded to n de$i#al pla$es% If n is o#itted then the !alue # is ounded to : de$i#al pla$es. n #ay be ne"ati!e to ound di"its left of de$i#al point% 'isplay e#ployee na#e. sala y. 11? of sala y as bonus ounded to nea whole nu#be % SQL = S+L+CT ena#e. sal. &OUN',sal @ :%11 . :0 5ONUS F&OM e#p; +NAM+ 6666666666 SMITA ALL+N 3A&' DON+S MA&TIN 5LAE+ CLA&E SCOTT EIN( TU&N+& A'AMS DAM+S FO&' MILL+& SAL 5ONUS 6666666666 6666666666 8:: 1:/ 1B:: ):8 1)C: 1B1 )4-C 181)C: 1B1 )8C: 1-1 )/C: 114 1::: 14: C::: BC: 1C:: 14C 11:: 1/1 4C: 1)/ 1::: 14: 11:: 1B4
SQL FUNCTIONS
Function Purpose
T$UNC (m,n) To t un$ate the !alue of the nu#e i$ e*p ession to the spe$ified p e$ision%>alue # is t un$ated to n de$i#al pla$es% If n is o#itted then the !alue # is t un$ated to : de$i#al pla$es. n9: t un$ates di"its left of de$i#al point% 'isplay e#ployee na#e. sala y. 11? of sala y as bonus t un$ated to nea whole nu#be % SQL = S+L+CT ena#e. sal. T&UNC,sal @ :%11 . :0 5ONUS F&OM e#p; +NAM+ 6666666666 SMITA ALL+N 3A&' DON+S MA&TIN 5LAE+ CLA&E SCOTT EIN( TU&N+& A'AMS DAM+S FO&' MILL+& 1/ ows sele$ted% SAL 5ONUS 6666666666 6666666666 8:: 1:/ 1B:: ):8 1)C: 1B) )4-C 18B 1)C: 1B) )8C: 1-: )/C: 118 1::: 14: C::: BC: 1C:: 14C 11:: 1/1 4C: 1)1 1::: 14: 11:: 1B4
SQL FUNCTIONS
C-"r"cter Functions
The followin" a e so#e of the $ha a$te fun$tions used to pe fo # #anipulations with st in"s Function Purpose !"mp#e $eturn %"#ue INITC'P(c-"r.strin/) To etu n the fi st lette of ea$h wo d. of the $ha a$te st in" spe$ified. in Uppe $ase and all othe s in lowe $ase% Na#e < Fdelhi spo tsG Init$ap,na#e0 < F'elhi Spo tsG
UPP $(c-"r.strin/) To etu n the spe$ified $ha a$te st in" in Uppe $ase%. in Na#e < Fdelhi spo tsG Uppe ,na#e0 < F'+LAI S2O&TSG
)O& $(c-"r.strin/) To etu n the spe$ified $ha a$te st in" in Lowe $ase% Na#e < F'+LAI S2O&TSG Lowe ,na#e0 < Fdelhi spo tsG
SQL FUNCTIONS
INST$(strin/,c-"r) To etu n the position of fi st o$$u en$e of $ha in st in" % Na#e < F'+LAI S2O&TSG Inst ,na#e.HSH0 < -
SU(ST$(c-"r.strin/,st"rt.position,no0 of c-"rs to be returne,) To etu n the spe$ified nu#be of $ha a$te s f o# the spe$ified position of the spe$ified st in" Na#e < Fdelhi spo tsG subst ,na#e.1./0 < FdelhG
) N*T1(strin/) To etu n the len"th of a st in"%% Na#e < F'+LAI S2O&TSG Len"th,na#e0 < 1)
SOUND 2(strin/) To etu n the phoneti$ ep esentation of ea$h wo d and allows you to $o#pa e wo d that a e spelt diffe ently but sounds aliIe% 'isplay the details of the e#ployee whose na#e is ALL+N o Alen% S+L+CT @ F&OM e#p 3A+&+ SOUN'+J,ena#e0<SOUN'+J,KAlenK0
SQL FUNCTIONS
$esu#t +M2NO +NAM+ DO5 M(& AI&+'AT+ 666666 6666666666 666666666 6666666666 666666666 -/44 ALL+N SAL+SMAN -B48 ):6F+5681 SAL COMM '+2TNO 6666666666 6666666666 6666666666 1B:: 1:: 1:
D"te Functions The followin" a e so#e of the date fun$tions used to pe fo # #anipulations with dates%
Function Purpose
Function Purpose
'DD.MONT1S(,,n) To etu n the spe$ified date with the spe$ified nu#be of #onths added% &etu ns date d plus n #onths% )'ST.D'3(,) &etu ns last day of the #onth $ontainin" the date d N 2T.D'3(,,c-"r) To etu n the date of the fi st weeIday spe$ified by $ha . afte the spe$ified date% MONT1S.( T& N(,,e)
Function Purpose
SQL FUNCTIONS
*ener"# Functions
Function Purpose
N%)(co#,v"#ue) To substitute a !alue in pla$e of NULL% The data type of the !alue to be Substituted #ust #at$h with $ol data type%
'isplay the a!e a"e $o##ission of the e#ployees% S+L+CT A>(,N>L, $o##.:00 F&OM e#p; A>(,N>L,COMM.:00 6666666666666666 1C-%1/)8C-
Function Purpose
D COD (C,%4,S4,%5,S5,60,D) To substitute on a !alue by !alue basis% It a$tually does a LIf6then6 elseH test %A default !alue $an also be spe$ified . whi$h is substituted . in $ase nothin" #at$hes f o# the set%HCH whi$h is eithe $olu#n o e*p ession is $o#pa ed to ea$h L>H and substitutes LSH if LCH e7uals the sea $h !alue% If no #at$h is found . the '+CO'+ fun$tion etu ns the L'H whi$h is default !alue% If the default !alue is o#itted. NULL is etu ned fo un#at$hed !alues%
1:
SQL FUNCTIONS
!"mp#e
'isplay the na#e.Mob of the e#ployees and display +*e$uti!e if the Mob is Cle I . &M if the Mob is Mana"e else display the Mob% S+L+CT ena#e.Mob. '+CO'+,Mob. LCL+&EH. L+J+CUTI>+H. LMANA(+&H. L&MH. Mob0 F&OM e#p; +NAM+ 6666666666 SMITA ALL+N 3A&' DON+S MA&TIN 5LAE+ CLA&E SCOTT EIN( TU&N+& A'AMS DAM+S FO&' MILL+& DO5 666666666 CL+&E SAL+SMAN SAL+SMAN MANA(+& SAL+SMAN MANA(+& MANA(+& ANALNST 2&+SI'+NT SAL+SMAN CL+&E CL+&E ANALNST CL+&E '+CO'+,DO 666666666 +J+CUTI>+ SAL+SMAN SAL+SMAN &M SAL+SMAN &M &M ANALNST 2&+SI'+NT SAL+SMAN +J+CUTI>+ +J+CUTI>+ ANALNST +J+CUTI>+
11
SQL FUNCTIONS
SUMM'$3 78
Fun$tions a e used to #anipulate data ite#s% Fun$tions $an #anipulate $ha a$te . nu#e i$ as well as date type of data% It $an pe fo # all so t of $o#ple* $al$ulations on data% Fun$tions $an be $ate"o ised in two types : Colu#n Fun$tion ( oup Fun$tion Colu#n Fun$tions $an fu the be $ate"o ised into A ith#eti$ Fun$tions. Cha a$te Fun$tions. 'ate Fun$tions and (ene al Fu$tions% 'ual table is a 'UMMN table whi$h $ontains one $ilu#n. 'UMMN. and one ow with a !alue J%
1)