FUNCTIONS
IN
MySQL
FUNCTIONS
Afunction is a special type of
predefined command set that
performs some operations
and returns a single value.
Single Row Function
SingleRow Function work with a
single row at a time. A single row
function returns a result for every
row of a queried table.
They are further categorized into:
Numeric functions
String functions
Date and Time functions
Mathematical/Numeric Functions
Mathematicalfunctions perform
mathematical operations on numeric
values. The most commonly used
mathematical functions are POW( ),
MOD(), ROUND().
POW( ) / POWER( ) - This function
returns a number m raised to the nth
power i.e. returns the argument
raised to the specified power.
For Example:-
(i)SELECT POWER(3,2) ;
(ii) SELECT POW(2,4);
(iii) SELECT POW(2,–2);
(iv) SELECT POW(–2,3);
ROUND( ) – This function returns a
number rounded off as per given
specifications.
ROUND(X) :- Rounds the argument X
to the nearest Integer
ROUND(X,D) Rounds the argument X
to D decimal places
e.g.mysql> ROUND(15.193 , 1) Result:-15.2
(ii) mysql> ROUND(–1.23); Result: –1
(ii) mysql> SELECT ROUND(–1.58); Result: –2
(iii) mysql> SELECT ROUND(1.58); Result: 2
(iv) mysql> ROUND(3.798, 1); Result: 3.8
(v) mysql> ROUND(1.298, 0); Result: 1
(vi) mysql> SELECT ROUND(23.298, –1); Result: 20
vii) mysql> SELECT ROUND(-1.23); Result: -1
viii) mysql> SELECT ROUND(-1.58); Result: -2
ix) mysql> SELECT ROUND(1.43); Result: 1
x) mysql> SELECT ROUND(6.298, 1); Result: 6.3
xi) mysql> SELECT ROUND(6.235, 0); Result: 6
xii) mysql> SELECT ROUND(56.235, -1); Result: 60
Xiii)mysql> SELECT ROUND(15.193 , -1); Result:20
TRUNCATE(): Truncates the argument
to specified number of decimal
places.
a) mysql> SELECT TRUNCATE(7.543,1); Result: 7.5
b) mysql> SELECT TRUNCATE(4.567,0); Result: 4
c) mysql> SELECT TRUNCATE(-7.45,1); Result: -7.4
d) mysql> SELECT TRUNCATE(346,-2); Result: 300
(e)mysql> SELECT TRUNCATE(7.29,1); Result: 7.2
(f) mysql> SELECT TRUNCATE(27.29,–1); Result: 20
MOD(): The MOD() function returns the remainder of one
number divided by another.
For example,
(i) SELECT MOD(11, 3); Result: 2
(ii) SELECT MOD(10.5, 3); Result: 1.5
(iii) SELECT MOD(11 , 4) ; Result: 3
(iv) SELECT MOD(25,7) Result:
4
(v) SELECT MOD(25,0) Result:25
(iv) SELECT MOD(-7,2) Result :-1
STRING FUNCTIONS
These functions are used to deal with
the string type values.
ASCII(): Returns the ASCII code value of a character.
mysql> SELECT ASCII('a') FROM DUAL; Returns 97.
mysql> SELECT ASCII('A') FROM DUAL; Returns 65.
mysql> SELECT ASCII('1') FROM DUAL; Returns 49.
mysql> SELECT ASCII('ABC') FROM DUAL; Returns 65.
• The ASCII value for upper case characters ‘A’ to ‘Z’ is 65 to 90.
• The ASCII value for lower case characters ‘a’ to ‘z’ is 97 to 122
and for digits ‘0’ to ‘9’, the ASCII value is 48 to 57.
CHAR() : Returns the corresponding ASCII character
for each integer passed.
Example : SELECT CHAR(65) ; Result : A
LOWER()/LCASE(): Converts
character strings data into lower
case.
For Example:-
1.mysql> SELECT LOWER("INFORMATION TECHNOLOGY");
Returns – information
technology
2. mysql>SELECT LOWER(“COLUMN NAME”);
Returns--- column name
UPPER()/UCASE(): Converts character
strings data into upper case.
For Example:
mysql>SELECT UPPER('information technology');
Returns – INFORMATION
TECHNOLOGY
mysql>SELECT UPPER(‘Publications');
Returns – PUBLICATIONS
LENGTH()/LEN() : Returns the length of
a string in bytes/no.of characters in
string.
For example,
i) mysql>SELECT LEN('Information Technology'); Returns:22
ii)mysql>SELECT LENGTH(‘Academic Council'); Returns:16
iii)mysql> SELECT LENGTH(‘CBSE COMPETITIVE EXAMINATIONS’);
Returns:29
iv) Mysql> SELECT LENGTH(“CANDID”) ; Returns:6
REPLACE(): Replaces all occurrences of the second string (string2)
in the first string (string1) with a third string (string3).
For Example:
i) mysql> SELECT REPLACE('INFORMATION TECHNOLOGY',
'INFORMATION','LATEST');
Returns – LATEST TECHNOLOGY
ii) mysql> SELECT REPLACE(‘COMPUTER SCIENCE’, ‘COMPUTER’,
‘SOCIAL’);
Returns---- SOCIAL SCIENCE
LEFT(Str, n) :-Returns the first n character from the string i.e.
Returns leftmost characters from a string, passed as an
argument, with the specified number of characters counting
from left.
For example,
i) mysql> SELECT LEFT('INFORMATION TECHNOLOGY', 6);
Returns:INFORM
ii) mysql> SELECT LEFT('Informatics', 3); Returns : 'Inf‘
iii) mysql>SELECT LEFT(‘PAPERLINE’,5); Returns:’PAPER’
RIGHT(): Returns the given number
of characters by extracting them
from the right side of the given
string.
For example:-
mysql> SELECT RIGHT('STRING FUNCTION', 8) Returns: FUNCTION
mysql> SELECT RIGHT('Informatics', 4); Returns:'tics‘
mysql> SELECT RIGHT(‘THINK GREEN’,4); Returns:’REEN’
LTRIM():- Removes leading spaces
i.e. removes spaces from the left
side of the string str.
For Example:-
mysql> SELECT LTRIM(' Informatics');
Returns: 'Informatics'
RTRIM( ) Removes trailing spaces i.e.
removes spaces from the right side
of the string str.
For Example:-
mysql> SELECT RTRIM ('Informatics
');
Result: 'Informatics'
TRIM():-Removes both leading and
trailing spaces from the string str.
For Example:-
mysql> SELECT TRIM(' Informatics ');
Result: 'Informatics'
REVERSE(): Returns reverse of an inputted string.
For Example:-
i) mysql> SELECT REVERSE('LIBRARY FUNCTION');
Returns –
NOITCNUF YRARBIL
ii) mysql> SELECT REVERSE(‘CREATE INSPIRE');
Returns—
ERIPSNI ETAERC
REPLICATE(): Repeats an inputted string for a specified number
of times.
For Example:-
mysql> SELECT REPLICATE('FUNCTION', 3);
Returns – FUNCTIONFUNCTIONFUNCTION
mysql> SELECT REPLICATE(‘SAFETY', 5);
Returns --SAFETYSAFETYSAFETYSAFETYSAFETY
SUBSTRING(str,m,n)/MID(str,m,n):-
Returns a substring starting from the
specified position in a given string.
For Example:
mysql> SELECT SUBSTRING('STRING FUNCTION', 1, 6);
Returns : STRING
mysql> SELECT SUBSTRING('STRING FUNCTION', 8, 8);
Returns: FUNCTION
mysql> SELECT MID('Informatics',3,4);
Returns: 'form'
a) mysql> SELECT SUBSTRING('Informatics',3);
Returns: 'formatics‘
b) mysql> SELECT SUBSTRING('Informatics' FROM 4);
Returns: 'ormatics‘
c) mysql> SELECT SUBSTRING('Informatics',3,4);
Returns: 'form'
d) mysql> SELECT SUBSTRING('Computers', -3);
Returns: 'ers'
e) mysql> SELECT SUBSTRING('Computers', -5, 3);
Returns: 'ute‘
f) mysql> SELECT SUBSTRING('Computers‘, FROM -4 FOR 2);
Returns: 'te'
Date/Time Functions
Dateand Time functions allow us to
perform many types of tasks on Date
type data.The default date format in
MySQL is YYYY-MM-DD.
CURDATE(): Returns the current
system date.
For example,
mysql>SELECT CURDATE();
Result: '2020-09-11'
NOW(): Returns the current date and
time.
For example,
mysql>SELECT NOW();
Result:
'2020-06-11 13:58:11'
SYSDATE(): Returns the time at
which the function executes.
For example,
mysql>SELECT SYSDATE();
Result: '2020-
06-11 13:59:23'
DATE(): Extracts the date part of a date or
date-time expression.
For example,
mysql>SELECT DATE('2020-06-11
01:02:03');
Result: '2020-06-11‘
MONTH(): Returns the month from
the date passed.
For example,
Mysql>SELECT MONTH('2020-06-11');
Result: 6
YEAR(): Returns the year from the
inputted date.
For example,
mysql>SELECT YEAR('2020-06-11');
Result: 2020
DAYNAME(): Returns the name of the
weekday.
For example,
mysql>SELECT DAYNAME('2020-06-11');
Result: THURSDAY
DAYOFMONTH(): Returns the day of
the month (0-31).
For example,
i)mysql>SELECT DAYOFMONTH('2020-06-11');
Result: 11
DAYOFWEEK(): Returns the weekday
index of the argument.
For example,
i) mysql>SELECT DAYOFWEEK('2020-06-11');
Result: 5
(Sunday is counted as 1)
ii)mysql> SELECT DAYOFWEEK('2020-08-31');
Result: 2
iii)mysql>SELECT DAYOFWEEK('2020-09-');
Result:
DAYOFYEAR(): Returns the day of the year (1-366).
For example,
i) mysql>SELECT DAYOFYEAR('2020-06-11'); Result:202
ii) mysql>SELECT DAYOFYEAR('2020-09-20'); Result:
AGGREGATE FUNCTIONS
1. MAX():- Returns the maximum/highest value among
the values in the given column/expression.
2. MIN():- Returns the minimum/lowest value among
the values in the given column/expression.
3. SUM():- Returns the sum of the values under the
specified column/expression.
4. AVG() :-Returns the average of the values under the
specified column/expression.
5. COUNT():- Returns the total number of
values/records under the specified column/expression.