Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
101 views

Functions in SQL

This ppt will guide us about how functions can be defined, declared and called in structure query language

Uploaded by

Amish Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
101 views

Functions in SQL

This ppt will guide us about how functions can be defined, declared and called in structure query language

Uploaded by

Amish Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

Functions in SQL

Functions
 Functions serve the purpose of manipulating data items and
returning a result.
Functions

Aggregate Scalar
Functions Functions
Numeric Functions
String Functions
Date Functions
Conversion Functions
Aggregate Function
 Functions that works on a set of values are called Aggregate
function or group function.
 AVG() : Returns an average value of ‘n’, ignoring null values in
a column.
 Syntax: AVG ( [ <DISTINCT> | <ALL> ] <n> )
 Example: select avg(cur_bal) "Avg balance" from acc_master;

 MIN() : Returns a minimum value of expr.


 Syntax: MIN ( [ <DISTINCT> | <ALL> ] <expr> )
 Example: select min(all cur_bal) "Min balance" from acc_master;
Continue…
 COUNT() : Returns number of rows ignoring null.
 Syntax: AVG ( [ <DISTINCT> | <ALL> ] <expr> )
 Example: select count(distinct cur_bal) "Min balance" from acc_master;
 COUNT(*): Returns number of rows in table including duplicates and
nulls.

 MAX() : Returns the maximum value of expr.


 Syntax: AVG ( [ <DISTINCT> | <ALL> ] <expr> )
 Example: select max(age) "Min balance" from customer1;
Continue…
 SUM() : Returns sum of values of column.
 Syntax: AVG ( [ <DISTINCT> | <ALL> ] <n> )
 Example: select sum(cur_bal) "Min balance" from acc_master;
Scalar functions
 Functions that works on only one value at a time are called
scalar function or single row function.
 Types are as follows:
 Numeric Functions
 String Functions
 Conversion Functions
 Date Functions
Numeric Functions
 ABS() : Returns an absolute value of ‘n’.
 Syntax: ABS( <n> )
 Example: select ABS (-15) “Absolute” from dual;
 Output:
Continue…
 POWER() : Returns m raised to n power.
 Syntax: ABS( <m> , <n>)
 Example: select power(3,4) "POWER" from dual;
 Output:

 ROUND() : Returns n, rounded to m places to the right of a


decimal point. If m is omitted, n is rounded to 0 place. m can be
negative to round off digits to left of decimal point.
 Syntax: ROUND( <n>, [<m>] )
 Example: select round(2.368,2) "ROUND" from dual;
 Output:
Continue…
 SQRT() : Returns square root of ‘n’. If n <0, error occurs.
 Syntax: SQRT( <n> )
 Example: select sqrt(144) "ROOT" from dual;
 Output:

 EXP() : Returns e raised to the n power. e= 2.17182813


 Syntax: EXP( <n> )
 Example: select exp(2) "Exponentiation" from dual;
 Output:
Continue…
 EXTRACT() : Returns a value extracted from a date or time.
 A date can be used to extract YEAR, MONTH, DAY.
 A time stamp can be used to extract HOUR, MINUTE, SECOND.
 Syntax: EXTRACT ({year | month | day | hour | minute | second }
from { date_value | time_value}
 Example: select extract(Year from date '2017-12-30') "Year",
extract(Month from date '2017-12-30') "Month", extract(Day from date
'2017-12-30') "Day" from dual;
 Output:

 select extract(Hour from current_timestamp) "Hour",extract(Minute


from current_timestamp) "Minute", extract(second from
current_timestamp) "Second" from dual;
Continue…
 GREASTEST() : Returns the greatest value in a list of
expression.
 Syntax: GREATEST (expr1, expr2, …. exprn)
 select greatest(7,15,8) "Greatest Number" from dual;

 select greatest('7','15','8') "Greatest String" from dual;


 LEAST() : Returns the least value in a list of expression.
 Syntax: LEAST(expr1, expr2, …. exprn)
 select least(7,15,8) "least number" from dual;

 select least('7','15','8') "least String" from dual;


Continue…
 MOD() : Returns the reminder of first number divided by
second number passed in parameter. If second number is 0, it
returns first number.
 Syntax: MOD ( <m>, <n>)
 Example: select mod(76,13) "MOD" from dual;
 Output

 TRUNC() : Returns a number which truncate to a certain


number of decimal places. If parameter is omitted, it truncate
the number to 0 decimal places.
 Syntax: TRUNC( <number> [ ,<decimal place>])
 Example: select trunc(2.368,2) "truncate" from dual;
 Output
Continue…
 FLOOR() : Returns the largest integer value that is equal to or
less than a number.
 Syntax: FLOOR( <number>)
 Example: select floor(12.768) "FLOOR" from dual;
 Output

 CEIL() : Returns the smallest integer value that is greater or


equal to a number.
 Syntax : CEIL( <number>)
 Example: select ceil(12.768) "CEIL" from dual;
 Output
String Functions
 LOWER() : Returns character with all letters in lower case.
 Syntax: LOWER(char)
 Example: select lower('Hi how are YOU') "Lower" from dual;
 Output

 INITCAP() : Returns a string with first letter of each word in


upper case.
 Syntax: INITCAP(char)
 Example: select INITCAP('Hi how are YOU') "Each Word Initial Capital"
from dual;
 Output
Continue…
 UPPER() : Returns character, with all letters in upper case.
 Syntax: UPPER(char)
 Example: select UPPER('Hi how are YOU') "UPPER CASE" from dual;
 Output

 SUBSTR() : Returns portion of string, begins with character


position m, going upto n characters. If n is omitted, it returns
upto last charcater of a string.The first position of character is 1
 Syntax: SUBSTR(<string>, <m> [,<n>])
 select substr('Hi how are YOU',3,4) "Sub string" from dual;
 select substr('Hi how are YOU',3) "Sub string" from dual;
Continue…
 ASCII() : Returns the ascii number of a character. If more
than one character is entered, it returns the ascii value of first
character ignoring rest characters.
 Syntax: ASCII(<single_char>)
 Example: select ascii('a') "ASCII" from dual;
 Example: select ascii('abcd') "ASCII" from dual;
Continue…
 INSTR() : Returns location of a substring in a string.
 Syntax: INSTR(<string1>,<string2> [,<start_position>]
[,<nth_appearance>]
 <string1> : String in which search is performed.
 <string2>: Substring to search for.
 <start_position>: Position in string1 from where start will search.
Default is 1. If negative , then start position counts from the end of string1
and search towards the beginning of string1.
 <nth_appearance>: nth appearanace of string2. If omitted, defaults to 1.

 select instr('Hi how are you','o',1,1) "Search Substr" from dual;


 select instr('Hi how are you','o',1,2) "Search Substr" from dual;
 select instr('Hi how are you','o',6,1) "Search Substr" from dual;
Continue…
 TRANSLATE() : Replaces a sequence of characters in a string
with another set of characters. It replace a single character at a
time.
 Syntax:
TRANSLATE(<string1>,<string_to_replace>,<replacement_string>)
 <string1>: String in which replacement will done.
 All the character of <string_to_replace> will be replaced with the
corresponding characters in the <replacement_string>.
 Example: select translate('Hi how are you','you','YOU') "REPLACE"
from dual;
 Output
Continue…
 LTRIM() : Removes characters from the left of char with
initial characters removed upto the first character not in set.
 Syntax: LTRIM (char[,set])
 Example: select LTRIM('HHHi how are you','H') "LTRIM" from dual;
 Output

 RTRIM() : Removes final characters after the last character


not in the set. Set is defaults to spaces.
 Syntax: RTRIM (char[,set])
 Example: select RTRIM('Hi how are youuuuu','u') "RTRIM" from dual;
 Output:
Continue…
 TRIM() : Removes all specified characters either from the
beginning or the ending of a string.
 Syntax: TRIM ([leading | trailing | both [<trim_character> FROM] ]
<string1>)
 leading: remove from front, trailing: remove from end, both: remove
from front and end & default value is both.
 select TRIM(leading '$' FROM '$$$Hi how are you$$$') "TRIM" from
dual;

 select TRIM('$' FROM '$$$Hi how are you$$$') "TRIM" from dual;
Continue…
 LPAD() : Returns char1, left-padded to length n with the
sequence of characters specified in char2. If not specified char2
then by default oracle take blanks.
 Syntax: LPAD(char1,n [,char2])
 Example: select LPAD('Hi',4,'$') "LPAD" from dual;
 Output:

 RPAD() : Returns char1, right-padded to length n with the


sequence of characters specified in char2. If not specified char2
then by default oracle take blanks.
 Syntax: RPAD(char1,n [,char2])
 Example: select RPAD('Hi',4,'$') "RPAD" from dual;
 Output:
Continue…
 LENGTH() : Returns the length of a word.
 Syntax: LENGTH(word)
 Example: select length('Hi how are you') "LENGTH" from dual;
 Output

 VSIZE() : Returns number of bytes in the internal


representation of an expression.
 Syntax: VSIZE(<expression>)
 Example: select vsize('Hi how are you') "vsize" from dual;
 Output:
Conversion Functions
 TO_NUMBER() : Converts a character which expressing a
number to a NUMBER data type.
 Syntax: TO_NUMBER(char)
 Example: select TO_NUMBER('12345') from dual;
 Output:

 TO_CHAR(number conversion) : Converts a value of a


NUMBER data type to a character data type using the specified
format. fmt is the number format in which the number has to
appear.
 Syntax: TO_CHAR(n [,fmt])
 Example: select TO_CHAR(16785,'$0999990') from dual;
 Output:
Continue…
 select TO_CHAR(16785,'$999') from dual;

 select TO_CHAR(16785) from dual;


Continue…
 TO_CHAR(date conversion) : Converts a value of a DATE
data type to a character data type using the specified format.
fmt is the date format in which the date has to appear.
 Syntax: TO_CHAR(date [,fmt])
 Example: select TO_CHAR('12-31-2016') "New Date Format" from
dual;
 Output:
 TO_DATE() : Oracle stores and display the date in default
format.To change the format of a date, this function is used.
 Syntax: TO_CHAR(char [,fmt])
 Example: select TO_DATE('12-01-2016','DD-MM-YYYY') "New Date
Format" from dual;
 Output:
DATE FUNCTIONS
 Format of date is ‘mm-dd-yyyy’.
 ADD_MONTHS() : Returns date after adding the number of
months given in function.
 Syntax: ADD_MONTHS(d,n) d=date, n=no of months
 Example: select Add_months(sysdate,6)"New Date" from dual
 Output:

 LAST_DAY() : Returns last date of the month specified with


the function.
 Syntax: LAST_DAY(d) d=date
 Example: select last_day('04-12-2016')"Last day" from dual
 Output:
Continue…
 MONTHS_BETWEEN() : Returns number of months
between d1 and d2.
 Syntax: MONTHS_BETWEEN(d1, d2) d1 & d2=date
 Example: select months_between('12-11-2016','10-11-2015')"Months"
from dual;
 Output:
 NEXT_DAY() : Returns the date of first weekday named by
char that is after the date named by date.
 Syntax: NEXT_DAY(date, char) d=date, char= day of a week
 Example: select next_day('08-09-2017','Sunday')"Date of Next Day"
from dual;
 Output:

You might also like