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./0 1 Function Purpose !"mp#e $eturn %"#ue 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+&.-.n) To dete #ine the #odulus of the fun$tion% &etu ns e#ainde of # di!ided by n MO'.)0 /4 1 .

1% SI(N.if n=:.-1//0 - Function Purpose !"mp#e $eturn %"#ue SI*N(n) To etu n the si"n of the "i!en nu#be %If n9:./0 1 / . :.6-0 - Function Purpose !"mp#e $eturn %"#ue C I)(n) To etu n the s#allest inte"e " eate than o e7ual to the "i!en nu#be % C+IL.SQL FUNCTIONS Function Purpose !"mp#e $eturn %"#ue '(S(n) To etu n the absolute !alue of the "i!en nu#be % A5S.61.-%1//0 8 Function Purpose !"mp#e $eturn %"#ue F)OO$(n) To etu n the la "est inte"e e7ual to o less than the "i!en nu#be % FLOO&.if n<:.

n #ay be ne"ati!e to ound di"its left of de$i#al point% 'isplay e#ployee na#e. sal. $esu#t C ./40 - !"mp#e $eturn %"#ue 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. etu ns null% di!ided by n SQ&T. if n9 :. +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 !"mp#e S+) Comm"n.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. sala y. :0 5ONUS F&OM e#p. &OUN'.sal @ :%11 . 11? of sala y as bonus ounded to nea whole nu#be % SQL = S+L+CT ena#e.

11? of sala y as bonus t un$ated to nea whole nu#be % SQL = S+L+CT ena#e.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. T&UNC. n9: t un$ates di"its left of de$i#al point% 'isplay e#ployee na#e.SQL FUNCTIONS Function Purpose T$UNC (m. sala y.sal @ :%11 . +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 !"mp#e S+) Comm"n. sal. :0 5ONUS F&OM e#p. $esu#t B .

na#e0 < F'elhi Spo tsG Function Purpose !"mp#e $eturn %"#ue UPP $(c-"r.strin/) To etu n the spe$ified $ha a$te st in" in Uppe $ase%. in Uppe $ase and all othe s in lowe $ase% Na#e < Fdelhi spo tsG Init$ap. of the $ha a$te st in" spe$ified. in Na#e < Fdelhi spo tsG Uppe .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.na#e0 < F'+LAI S2O&TSG Function Purpose !"mp#e $eturn %"#ue )O& $(c-"r.na#e0 < Fdelhi spo tsG - .strin/) To etu n the spe$ified $ha a$te st in" in Lowe $ase% Na#e < F'+LAI S2O&TSG Lowe .strin/) To etu n the fi st lette of ea$h wo d.

strin/.na#e.SQL FUNCTIONS Function Purpose !"mp#e $eturn %"#ue INST$(strin/.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#e0 < 1) Function Purpose !"mp#e S+) Comm"n.position.KAlenK0 8 .ena#e0<SOUN'+J.na#e.st"rt.HSH0 < - Function Purpose !"mp#e $eturn %"#ue SU(ST$(c-"r.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 . 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./0 < FdelhG Function Purpose !"mp#e $eturn %"#ue ) N*T1(strin/) To etu n the len"th of a st in"%% Na#e < F'+LAI S2O&TSG Len"th.1.

afte the spe$ified date% MONT1S.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 S3SD'T To etu n the $u ent date and ti#e Function Purpose 'DD..( T& N(.D'3(..MONT1S(.c-"r) To etu n the date of the fi st weeIday spe$ified by $ha ..e) Function Purpose Function Purpose Function Purpose To etu n the nu#be of #onths between dates d and e% 4 .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.) &etu ns last day of the #onth $ontainin" the date d N 2T.D'3(.

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: .%4.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 .60. whi$h is substituted . A>(.COMM. $o##.%5.SQL FUNCTIONS *ener"# Functions Function Purpose N%)(co#.N>L. $esu#t 'isplay the a!e a"e $o##ission of the e#ployees% S+L+CT A>(. 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 .N>L.S5.:00 F&OM e#p.S4.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% !"mp#e S+) Comm"n.:00 6666666666666666 1C-%1/)8C- Function Purpose D COD (C.

LCL+&EH.SQL FUNCTIONS !"mp#e 'isplay the na#e.Mob.Mob of the e#ployees and display +*e$uti!e if the Mob is Cle I . L+J+CUTI>+H. LMANA(+&H. '+CO'+.Mob. +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'+. Mob0 F&OM e#p. L&MH. &M if the Mob is Mana"e else display the Mob% S+L+CT ena#e. $esu#t 11 .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>+ S+) Comm"n.

Cha a$te Fun$tions. 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. 'UMMN.SQL FUNCTIONS SUMM'$3 78 • Fun$tions a e used to #anipulate data ite#s% Fun$tions $an #anipulate $ha a$te . and one ow with a !alue J% • • • • • 1) . 'ate Fun$tions and (ene al Fu$tions% 'ual table is a 'UMMN table whi$h $ontains one $ilu#n.