You are on page 1of 3

Practical No: 5

Perform MySQL Built-In functions (mathematical, character functions)


Theory:
MySQL can do much more than just store and retrieve data. We can also perform
manipulations on the data before retrieving or saving it.
The built-in MySQL math functions allow programmers to conduct mathematical
operations on numerical data that is stored in the database. These functions can be used in
SQL queries to carry out a number of calculations, such as determining a number's absolute
value, rounding it to a given decimal point, or determining its square root.
There are a number of built-in functions in MySQL, including functions for string,
date, and numeric values.
Functions allow us to enhance the capabilities of MySQL.
Functions always return a value and can optionally accept parameters.
Built in functions are functions that are shipped with MySQL. They can be
categorized according to the data types that they operate on i.e. strings, date and numeric built
in functions.

Types of functions Built-in functions


 Strings functions – operate on string data types
 Numeric functions – operate on numeric data types
 Date functions – operate on date data types
 Aggregate functions – operate on all of the above data types and produce
summarized result sets.
 Other functions – MySQL also supports other types of built in functions but we will
limit our lesson to the above named functions only.

MySQL Numeric Functions


Sr. Function Usage Example Result
no name
1 ROUND() Rounds a number SELECT Round(3.14159, 2); 3.14
to a specified
number of decimal
places.
2 FLOOR() Rounds a number SELECT Floor(3.8); 3
down to the
nearest integer.
3 MOD() Provides the SELECT Mod(7, 3); 1
remainder of a
number divided by
another number.
4 CEIL() rounds a given SELECT Ceil(44.6); 45
number up to the
nearest integer.
5 ABS() Absolute value SELECT Abs(-10) 10
function returns
the absolute value
of a number.
6 EXP() Returns the value SELECT Exp(2) 7.3890560989306495
of the constant e
raised to the power
of a specified
number.
7 PI() pi function returns SELECT Pi() 3.141592653589793
the value of pi to
16 decimal places

// Students must add minimum of 5 more Numeric Built-in Functions w.r.t application
database.

MySQL String Functions


Sr. Function Usage Example Result
no name
1 MATCH() searches a string SELECT Match('Hello World', 7
for a pattern and 'World');
returns the position
of the match.
2 LOWER() returns a string SELECT Lower('Hello World'); 'hello world'
with all characters
converted to
lowercase.
3 UPPER() returns a string SELECT Upper('Hello World'); 'HELLO WORLD'
with all characters
converted to
uppercase.
4 LEFT() returns a specified SELECT LEFT('Hello World', 5); Hello'
number of
characters from the
beginning of a
string.
5 RIGHT() extracts a specified SELECT RIGHT('apple', 3); 'ple'
number of
characters from the
right side of a
string.
6 LOCATE() returns the position SELECT Locate('o', 'Hello World'); 5
of the first
occurrence of a
substring in a
string.
7 REPEAT() returns a string SELECT Repeat('', 3);
consisting of a
specified number
of repetitions of
another string.
8 CONCAT function always SELECT computerscience
appends CONCAT('computer' ,'science')
( concatenates ) FROM DUAL;
string2 to the end
of string1
9 LENGTH returns the length SELECT LENGTH('Learning Is 15
of the input string Fun') FROM DUAL;
10 SUBSTR returns a portion of SELECT SUBSTR('Database Managem
a string from a Management System', 9, 7) FROM
given start point to DUAL;
an end point. If a
substring length is
not given, then
SUBSTR returns
all the characters
till the end of
string (from the
starting position
specified).
11 LPAD SELECT LPAD('100',5,'*') FROM **100
DUAL;
12 RPAD SELECT RPAD('5000',7,'*') FROM 5000***
DUAL;
13 TRIM trims the string SELECT TRIM('D' FROM atabase
input from the start 'Database') FROM DUAL;
or end (or both)
// Students must add minimum of 5 more Character Built-in Functions w.r.t application
database.

MySQL - Date and Time Functions

TIMESTAMP()

ADDDATE()

CURDATE()

DATE()

DAY()

STR_TO_DATE()

// Students must Write minimum of 10 date and time Built-in Functions w.r.t application
database.

You might also like