Functions in MySQL
Functions in MySQL
Using SQL
4.1 INTRODUCTION
Syntax:
SELECT <function>(colunmn_name) FROM <table_name>;
While working with standard library functions, the following points must be keat in
(a) The
kept
in mind
name of the column on which the function is to be executed must be
parentheses. closed within
enclosed .
Functions
Single Row
Functions Multiple Row
Functions
Fig. 4.2: Classification of MySQL Functions
4.3 SINGLE ROW FUNCTIONS
Single row functions operate on a
single row to return a single
value per row as the
output,IThey can accept one or more
arguments but return
only one result per row. These are also
known asScalar Functions. When Scalar Function
(i)
SELECT POW(2,-2);
Result: -8
(ii) SELECT POW(-2,3);
1 2 3 Decimal places
-2-1 0 of values
different sets
function for
behaviour of ROUND0
the
below exhibit
The situations
given
If the
result is a n integer.
for x and d: decimal and the
rounded up to 0 digits of n u m b e r is
i n c r e a s e d by
1.
x is
Ifd is not specified:
>5, then the integer
() decimal value is 5 or
the
first digit after For example,
nothing is specified.
0 if
Default decimal place is Result: 2
.58);
mysql>SELECT
ROUND (1 Result:-1
mysql>SELECT ROUND( - 1 . 2 3 ) ;
result is an integer value
decimal and the
rounded up to
0 digits of
If d is 0: x is
ii
Result: 1
For example,
(1.298, 0);
m y s q l > S E L E C T
ROUND
Result: -6
mysql>SELECT ROUND(-5.898, 0);
(d+1)th digit;
after the decimal by checking
d digits
up to float.
the result is
rounded a
is
by 1 and
x
integer:
positive increased
ii) Ifd is a
dth digit is
then the
if it is 5 or > 5,
Result: 3.799
For example, 3);
(3.79867,,
ROUND
checking the
m y s q l > S E L E C T
integer:
x is
is
increased by 1,
If d is a negative
then the
(d-1)" digit s e c o n d argument
is negative,
(iv) >5, value. So, if the
is 5 or
integer
dth digit; if it the resultis
an
-1000 respectively.
ero
and
the
nearest 10, 100,
converted to off to
rounds
then it
-3,
i.e., -1, -2,
For example,
Result: 20
-1);
(23.298,
Mysql>SELECT
ROUND Result: 40000
(36567.78,
-4);
Mysql>SELECT
ROUND
ROUND(-1.23)
| ROUND(1.298, 0)
ROUND(-5.898, 0)
-6
3.799
- ---*-+
mysql> SELECT
ROUND(36567.78, -4);
-------
ROUND (36567.78,-4) - -
40000
in set
row
(0.00 sec)
mysql>
MOD): TheMOD)
the syntax of the MOD) function
following is
another: The fol
The
ther
M O D [ d i v i d e n d , divisor);
SELECT
Syntax: SEL
Syntax: or a numeric expression to divide
number
Is a literal
dividend- dividend is to he divided
number or a numeric expresSion by which the
ar - Is a literal
divisor
Result: 2
mysql>SELECT MOD (11, 3);
Eorexample, () Result: 1.5
(ii) mysql>SELECTMOD (10.5, 3);
argument
returns the squareroot value of the specified
numeric
The ASCII value for upper 7 1s 97 to 122 and for digits O to the
case
characters 'a to
lower
T h e ASCII value for
57.
ASCIl value is 48 to is dummy talble usei for
uses Dual table which a
then SQL
Note: If no table name is specified, value X.
one-row,
one-column Dummy with a
operations. It is
a
performing
data into lower case.
0LowER)/LCASE):
Converts character strings
LOWER(String);
Or LCASE(String);
Syntax: SELECT
(d) LENGTH(): Returns the length of the character string. It takes spaces
into account for
calculating the total length of the string passed as an between the cs
Syntax: SELECT LENGTH(String); argument to LENOstrings
to LENGTH)
For example,
mysql> SELECT
LTRIM ((
Returns- LIBRARY FUNCTION LIBRARY FUNCTION');
argumeni ent.
as an
passed
string
S y n t a x :S E L E C T R T R I M ( S t r i n g ) ;
F o re x a m p l e ,
FUNCTION
RTRIM("LIBRARY
mysql>
SELE
Returns-LIBRARYFUNCTION
all the
as an argument after removing
functions return the string passed
RIM) and RTRIM()
in between the strings.
CTM L Buttthey do not delete the spaces
spaces.
after removing
Returns a string
TRIMO:
argument
to it.
as the
Syntax: SELECT TRIM(String);
Returns
reverse of an inputted string.
REVERSE():
k) Syntax: SELECT REVERSE(String);
FUNCTION');
('LIBRARY
REVERSE
SELECT
mysql>
Returns NOITCNUF YRARBIL
number of times.
specified
string for
a
an inputted
REPEAT(O: Repeats
() REPEAT(String,
Integer);
SELECT
Syntax: 3);
('FUNCTION',
REPEAT
sELECT
Returns
mysql> FUNCTIONFUNCTIONFUNCTION SUBSTRING)
A
of inputted string.
an character
Returns part specified
at the
string starting
SUBSTRINGO/MIDO/SUBSTR):
OR Length);
Start_index,
SUBSTR(String,
SELECT
OR Length);
Start_index,
MID(String, FUNCTION',
1, 6);
SELECT
SUBSTRING('STRING
sELECT
mysql>
Returns -STRING FUNCTION',
8, 8);
('STRING
SUBSTR
SELECT
mysql>
Returns - FUNCTION
FUNCTION',
3,4);
('STRING
MID
SELECT
mysql>
Returns- RING
SQL 4.7
Database Query using
of the substrino
INSTRO: Returns the position of the first occurrence
in
(n) the
Returns 0 if the substring is not present in the string giVen
string
Syntax: SELECT INSTR(String, Substring);
mysql> Informa ")
Returns- 6
(o) CONCAT): Returns the string that results from concatenating the
arguments, M.
or more arguments. Returns NULL if any argument is NULL. May have tte
yntax: SELECT CONCAT(String1, String2, String3...String n);
mysql> CTI actices', "Class-XII');
Returns- InformaticsPracticesClass-XII
(g) YEAR('2021-02-03');
DAYNAME(): Returns the name of the Result: 2021
For example, weekday.
mysql>E AZ11AME ('2021-02-0
(h)
DAYOFMONTHO: Returns the day of the month Result: Wednesday
For example, (0-31).
mysql>E
()
MONTHNAMEQ: AFMITE(2021-02-0'
Returns the name of
the
); Result: 3
For example, month (January, February..-)
mysql> SELECT
MONTHNEME ('2021-02- Result: February
4.8 Informatics Practices with
Python-XII
Returns the weekday index of the argument.
DAYOFWEEK():
For example,
mysql> SELECT D A Y O F W E E K ( ' 2 0 2 1 - 0 2 - 0 3 ) Result: 4 (Sunday is counted as 1)
(k)
For example, Result: 34
SELECT
DAYOFYEAR ("2021-02-03');
mysql>