PLSQL Functions
PLSQL Functions
SQLprovidesvariousinbuiltfunctionstosimplifysignificantlylargetasks.SQL
functionscanbebroadlyclassifiedas:
GroupFunctionsThishelpsingroupingofrowsreturnedbythequery.Thisisusedto
computeaggregatevaluessuchasSum,Averageetc.andhenceisalsocalledasAggregate
Function.
SingleRowFunctionsThesearefurtherclassifiedasNumericfunctions,DateFunctions,
ConversionfunctionsandCharacter/Textfunctions.
1. GROUPFUNCTIONS:
Generalsyntaxforusingaggregatefunctionsis:
SELECTX,AVG(Y)FROMTABLEGROUPBYX;whereXandYaretwocolumnsof
table
AVG(x)computestheaveragevalueofagivensetofvalues
COUNT(x)countsrowsinspecifiedtableorgivencolumn
MIN(x)providesminimumvaluefromasetofvalues
MAX(x)providesmaximumvaluefromasetofvalues
SUM(x)computesthesumofvalues
2. SINGLEROWFUNCTIONS:
Belowarementionedallthetypesofsinglerowfunctions:
NumericFunctions:
ABS(y)Providesabsolutevalueofy
SELECTABS(89.78)FROMDUAL;
Outputofquery>89.78
CEIL(y)Providesvaluegreaterthanorequaltoy
SELECTCEIL(2.56)FROMDUAL;
Outputofquery>3
SELECTCEIL(1.4)FROMDUAL;
Outputofquery>1
FLOOR(y)Providesvaluelessthanorequaltoy
SELECTCEIL(2.56)FROMDUAL;
Outputofquery>2
SELECTCEIL(1.4)FROMDUAL;
Outputofquery>2
TRUNC(x,y)Truncatesthevalueofxuptoydecimalplaces
SELECTTRUNC(25.67,1)FROMDUAL;
Outputofquery>25.6
ROUND(x,y)Providesroundedoffvalueofxuptoydecimalplaces
SELECTROUND(25.67,1)FROMDUAL;
Outputofquery>25.7
ConversionFunctions:
NVL(x,y)Replacexwithyincasexisnullwherexandyareofsamedatatype
SELECTNVL(null,3)FROMDUAL;
Outputofquery>3
DECODE(x,y,z,defaultvalue)Checksifx=y,thenreturnszelsereturnsdefaultvalue
SELECTDECODE(1,1,2,3)FROMDUAL;
Outputofquery>2
TO_CHAR(x,y)convertsxintotheformatspecifiediny
SELECTTO_CHAR(5000,$9999)FROMDUAL;
Outputofquery>$5000
TO_DATE(x,date_format)convertsvalueofxtoavaliddateformatspecifiedbydate_
format
Outputofquery>'2012-07-18
Character/TextFunction:
LOWER(StringValue)convertscompletestringtolowercase
SELECTLOWER(HelloWorld)FROMDUAL;
Outputofquery>helloworld
UPPER(StringValue)convertscompletestringtouppercase
SELECTUPPER(HelloWorld)FROMDUAL;
Outputofquery>HELLOWORLD
INITCAP(StringValue)makesthefirstletterappearincapitalcase
SELECTINITCAP(HelloWorld)FROMDUAL;
Outputofquery>HelloWorld
LTRIM(StringValue,TrimValue)TrimValuetextisremovedfromtheleftofString
SELECTLTRIM(HelloWorld,Hello)FROMDUAL;
Outputofquery>World
RTRIM(StringValue,TrimValue)TrimValuetextisremovedfromtherightofString
SELECTRTRIM(HelloWorld,World)FROMDUAL;
Outputofquery>Hello
SUBSTR(StringValue,m,n)Returnsnnumberofcharactersstartingfrompositionm
SELECTSUBSTR(HelloWorld,7,5)FROMDUAL;
Outputofquery>World
LENGTH(StringValue)ProvidesthetotallengthofStringValue
SELECTLENGTH(HelloWorld)FROMDUAL;
Outputofquery>11
LPAD(StringValue,n,PadValue)ProvidesStringValueleftpaddedwithpadvalueand
lengthn
SELECTLPAD(Hello,6,$)FROMDUAL;
Outputofquery>$Hello
RPAD(StringValue,n,PadValue)ProvidesStringValuerightpaddedwithpadvalueand
lengthn
SELECTRPAD(Hello,6,$)FROMDUAL;
Outputofquery>Hello$
REPLACE(StringValue,x,y)replaceseveryoccurrenceofxwithy
SELECTREPLACE(Hello,H,C)FROMDUAL;
Outputofquery>Cello
TRIM(TrimTextfromStringValue)RemovesalloccurrenceofTrimTextfromleftandright
ofStringValue.WhereTrimTextcanalsobeasinglecharacter
SELECTTRIM(ofromHelloWorld)FROMDUAL;
Outputofquery>HellWrld
DateFunctions:
ADD_MONTHS(date,n)Addsnmonthstothedatevalue
SELECTADD_MONTHS(26Sep86,3)FROMDUAL;
Outputofquery>26Dec86
MONTHS_BETWEEN(y1,y2)returnsthenumberofmonthsbetweentwodates
SELECTMONTHS_BETWEEN(26Sep86,26Dec86)FROMDUAL;
Outputofquery>3
NEXT_DAY(x,WeekDay)returnsthenextdateoftheWeekDayonoraftertheoccurrence
ofdatex
SELECTNEXT_DAY(02Oct17,Wednesday)FROMDUAL;
Outputofquery>04Oct17
LAST_DAY(x)thisprovidesthedateofthelastdayofmonthcontainingdatex
SELECTLAST_DAY(02Oct17)FROMDUAL;
Outputofquery>31Oct17
NEW_TIME(x,zone1,zone2)returnsthetimeinzone2ifxrepresentstimeinzone1
SELECTNEW_TIME(01Jun08,IST,EST)FROMDUAL;
Outputofquery>31May08
SYSDATEprovidesthecurrentdateandtime
CONCLUSION:
AbovementionedareimportantinbuiltfunctionsinSQL.Thesefunctionscanbeeasilyincorporated
tocomputeseveraldifferentlogicaloperationsbasedonqueryrequirement.
You cand read more about PL/SQL language on the site https://www.plsql.co .