Chapter 6 MySQL Functions
Chapter 6 MySQL Functions
MySQL Functions
Introduction
Function – A Function is a special type of predefined command set that performs
some operation and returns a single value.
The values that are provided to functions are called parameters or arguments.
There are two types of functions
1. Single Row functions - Works on each individual row of a table and return
result for each row. For example, ucase() , lcase(), round() etc. are single-row
functions.
2. Multiple row functions/Group function/Aggregate functions/ - group
functions or aggregate functions work upon group of rows and return one result for
the complete set of rows. For example sum(), avg(), count() etc. are multiple row
functions.
In this chapter we shall discuss about Single row functions.
Text/String Functions: The string functions of MySql can manipulate the text
string in many ways. Some commonly used string functions are :
(1) UCASE(str) / UPPER(str) – Returns the argument in uppercase i.e. it
changes all the characters of the passed string to uppercase.
(5) LEFT(str,n) – Returns the specified number of characters (n) from the left
side of string (str).
(6) RIGHT(str,n) – Returns the number of characters (n) form the right side of
string (str).
(7) INSTR(str,substr) Returns the position of the first occurrence of substring
<substr> in the string <str>.
(8) LTRIM(str) - Removes leading spaces, i.e., removes spaces from the left side
of the string <str>.
9. RTRIM(str) - Removes trailing spaces, i.e., removes spaces from the right side
of the string <str>.
10. TRIM(str) - Removes both leading and trailing spaces from the string.
TRIM() –
Example 1 - Write a query to remove leading spaces from string ‘ Bar One’.
Make use of TRIM() function only.
7. DAYNAME (date) - It returns the name of the weekday name for the
specified date.