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

Chapter 6 MySQL Functions

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

Chapter 6 MySQL Functions

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

Chapter – 6

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.

(2) LOWER(str) / LCASE(str) – Returns the argument(str) in lowercase i.e. It


changes all the characters of the passed string to lowercase.
(3) SUBSTRING(str,m,n) or SUBSTR(str,m,n) or MID(str,m,n) – Returns
<n> characters starting from the mth character of the string <str>.
If the third argument <n> is missing, then starting from the mth position, the rest
of the string is returned. If <m> is negative, the beginning of the substring is the
mth character from the end of the string.
(4) LENGTH(str) - Returns the length of a column or a string in bytes.

(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.

Example 2 - Write a query to remove leading x characters from string


‘xxxxBar One xxxxx’.
Example 3 - Write a query to remove both leading and trailing x characters from
string ‘xxxxBar One xxxxx’.

Example 4 - Write a query to remove trailing x characters from string


‘xxxxBar One xxxxx’.

1. Numeric Functions: MySQL numeric functions perform operations on numeric


values and return numeric values.
1. POWER(x,y) OR POW(x,y) - Returns the value of x raised to the
power of y.

2. ROUND(x) - Round the argument x to the nearest INTEGER.


ROUND(x,d) - Rounds the argument x to d decimal places.
3. MOD() - returns the remainder of a number divided by another number. This
function also works on fractional values and returns the exact remainder. The
function returns NULL when the value of divisor is 0.

Date/Time Functions: Date functions operate on values of the DATE datatype.


1. NOW() - Returns the current date and time in 'YYYY-MM-DD HH:MM:SS'.

2. DATE(expr) - Extracts the date part from a datetime expression.


3. MONTH(date) - Returns the numeric month from the date passed.

If current date is : ‘2020-04-20’


4. MONTHNAME(date) - Returns the month name form a date.
For example , If current date is : ‘2020-04-20’

5. YEAR(date) - Returns the year for date passed.


6. DAY() - returns the day of the month for a specified date. The day
returned will be within the range of 1 to 31.

7. DAYNAME (date) - It returns the name of the weekday name for the
specified date.

to display current date on your system.

You might also like