0% found this document useful (0 votes)
16 views

MySql Functions

SQL

Uploaded by

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

MySql Functions

SQL

Uploaded by

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

Functions in MySQL

Data can be manipulated in queries through functions supported by SQL. A function is a special
type of pre-defined command set that performs some operations and returns a single value.
Functions operate on zero or one or two or more values which are provided to them. The values
that ate provided to a function are called as parameters or arguments.
All the functions supported by MySql are broadly categorised into two types –
1. Single Row / Scalar Functions
2. Multi Row / Group / Aggregate Functions

Single Row / Scalar Functions


The single row functions may accept one or more arguments but returns only one value. While
used on database tables these functions return one result per row. These functions accept
arguments that can be column or value. All the single row functions in MySql are categorized as
i. String Functions ii. Numeric Functions iii. Date and Time Functions
❖ All function names used in MySql are not Case-sensitive.
❖ In MySql, string is any sequence of characters or a single character or blank space enclosed within single
quotation mark.

String Functions
String functions operate on strings (character type data). These are used to extract, change,
format the strings. Strings functions accept a string as argument but return either character
value or numeric value. Some commonly used string functions are given below –
ASCII ( )
This function takes a string as argument and returns the ASCII value of the left most character
of the given string. If the string is empty, it returns 0 (zero).
Syntax:
Select ASCII(string);
Example:
select ASCII('Amazon'); Returns 65 as output
select ASCII('amazon'); Returns 97 as output
select ASCII(' '); Returns 0 as output
we can use ASCII() on database tables.
Select ASCII(column_name) from table_name;
CHAR ( )
This function takes integer (one or more) as argument and returns the respected character for
that integer.
Syntax:
Select CHAR(N1, N2, ……); where N1, N2, … are integer values
Example:
select CHAR(121); Returns y as output
select ASCII(70, 65, 67, 69); Returns FACE as output

19
CONCAT ( )
This function concatenates two strings i.e., joins a string at the end of the given string.
Syntax:
Select CONCAT(string1, string2);
Example:
select CONCAT(‘Java ‘, ‘Tutorials’); Returns Java Tutorials as output
CONCAT_WS ( )
Here, WS stands for With Separator. This function concatenates two or more strings with a
separator (a symbol) specified by the user.
Syntax:
Select CONCAT_WS(separator, string1, string2);
Example:
select CONCAT_WS(‘-‘, ‘Left’, ‘to’, ‘Right’); Returns Left-to-Right as output
select CONCAT_WS(‘:’, ‘HH’, ‘MM’, ‘SS’); Returns HH:MM:SS as output
INSTR ( )
The meaning is INSTRING. This function returns the position (a numeric value) of first occurrence
of substring in the given string. If the substring is not present in the given string it returns 0
(zero).
Substring means part of the string.
Syntax:
Select INSTR(string, substring);

Example:
select INSTR('Information', 'for'); Returns 3 as output
select INSTR('Information', 'far'); Returns 0 as output
LCASE ( ) / LOWER ( )
This function converts all the alphabets present in a string to lower case. If those are already in
lower case there will be no change.
Syntax:
Select LCASE(string);
OR
Select LOWER(string);
Example:
select LCASE('HELLO WORLD'); Returns hello world as output
select LOWER('HELLO WORLD'); Returns hello world as output
select LCASE(‘HeLlO WoRlD’); Returns hello world as output
select LOWER(‘HeLLo 3697 WorLD!’); Returns hello 3697 world! as output
UCASE ( ) / UPPER ( )
This function converts all the alphabets present in a string to upper case. If those are already in
upper case there will be no change.
Syntax:
Select UCASE(string);
OR

20
Select UPPER(string);
Example:
select UCASE('hello world'); Returns HELLO WORLD as output
select UPPER(‘hello world’); Returns HELLO WORLD as output
select UCASE(‘HeLlO WoRlD’); Returns HELLO WORLD as output
select UPPER(‘HeLLo 3697 WorLD!’); Returns HELLO 3697 WORLD! as output
LTRIM ( )
This function removes the leading spaces (if any) i.e., spaces from the left of given string.
Syntax:
Select LTRIM(string);
Example:
select LTRIM(' MySql ');
RTRIM ( )
This function removes the trailing spaces (if any) i.e., spaces from the right of given string.
Syntax:
Select RTRIM(string);
Example:
select RTRIM(' MySql ');
TRIM ( )
This function removes both the leading and trailing spaces (if any) from the given string.
Syntax:
Select TRIM(string);
Example:
select TRIM(' MySql ');
LENGTH ( )
This function returns the length (an integer value) of given string i.e., total no of characters
(including blank spaces) present in the given string.
Syntax:
Select LENGTH(string);
Example:
select LENGTH('Hello World!’); Returns 12 as output
✓ In string one blank space is treated as 1 character.
REVERSE ( )
This function reverses the given string.
Syntax:
Select REVERSE(string);
Example:
select REVERSE('My Sql’); Returns lqS yM as output
LEFT ( )
This function is used to extract a specified number of characters from the left side of a given
string. It accepts two arguments. The second argument decides how many characters to be
extracted.
Syntax:

21
Select LEFT(string, n); where ‘n’ is the no of characters to be extracted
Example:
select LEFT('Fundamentals’, 3); Returns Fun as output
RIGHT ( )
This function is used to extract a specified number of characters from the right side of a given
string. It accepts two arguments. The second argument decides how many characters to be
extracted.
Syntax:
Select RIGHT(string, n); where ‘n’ is the no of characters to be extracted
Example:
select RIGHT('Colours’, 4); Returns ours as output
SUBSTR ( ) / MID ( )
This function is used to extract a substring i.e., part of a given string.
Syntax:
SUBSTR (string, m, n);
OR
MID(string, m, n)
where ‘m’ specifies the starting character of substring and ‘n’ specifies the lenght of substring.
If we don’t specify ‘n’ , it will return the substring from position ‘m’ to the end.
Example:
select SUBSTR('information’, 3,6); Returns format as output
select MID('information’, 3,6); Returns format as output

Numeric Functions
Numeric functions perform operations on numeric values and also return numeric values i.e.,
numeric functions accept numeric values as arguments and returns numeric value. These
functions are also called as ‘Mathematical Functions’. Some commonly used numeric functions
are given below –
MOD ( )
This function accepts two numbers as arguments and returns the remainder of division of two
numbers.
Syntax:
Select MOD(m, n); where ‘m’ and ‘n’ are two numbers
Example:
select MOD(14, 3); Returns 2 as output
POW ( ) / POWER ( )
This function accepts two numbers as arguments and returns the value of a number raised to
the power of another number.
Syntax:
Select POW(m, n); i.e., mn
OR
Select POWER(m, n);
Example:

22
select POW(4, 3); Returns 64 as output
select POWER(4, 3); Returns 64 as output
ROUND ( )
This function accepts a decimal (floating point) number and returns a decimal number after
rounding off up to a specific decimal place.
Syntax:
Select ROUND(m, d);
where ‘m’ is the decimal number and ‘d’ is specified decimal place up to which the number will
be rounded off.
Example:
select ROUND(15.325,2); Returns 15.33 as output
select ROUND(15.323,2); Returns 15.32 as output
select ROUND(-15.325,2); Returns -15.33 as output
select ROUND(-15.323,2); Returns -15.32 as output
If we do not specify any decimal place then it will round off to nearest integer.
Example:
select ROUND(15.325); Returns 15 as output
TRUNCATE ( )
This function accepts a decimal number and returns a decimal number after truncating it up to
a specific decimal place.
Syntax:
Select TRUNCATE(m, d);

Example:
select TRUNCATE(15.325); Returns 15.32 as output
SQRT ( )
This function accepts a number and returns the square root of the given number.
Syntax:
Select SQRT(n);
Example:
select SQRT(100); Returns 10 as output

DATE-TIME Functions
These functions allow us to perform various operations on DATE and TIME type data such as –
finding current date, day of a given date, carry out addition, subtraction of days etc. Some
commonly used DATE-TIME functions are given below –
CURDATE ( ) / CURRENT_DATE ( )
This function returns the current date in the format YYYY-MM-DD or YYYYMMDD depending on
whether the function is used in a string or in numeric context.
Syntax:
Select CURDATE ( ); OR Select CURRENT_DATE ( );
Here, no argument is to be passed.
Example:

23
Select CURDATE( ); Returns 2022-10-25 as output
Select CURRRENT_DATE( ); Returns 2022-10-25 as output
Select CURDATE()+5; Returns 20221030 as output
DATE ( )
This function retrieves the date from a given DATE-TIME value.
Syntax:
Select DATE(‘expression’); where expression is a date-time value
Example:
Select DATE(‘2022-01-01 05:10:15’); Returns 2022-01-01 as output
MONTH ( )
This function returns the month number from a given date value.
Syntax:
Select MONTH(‘date-value’);
Example:
Select MONTH(‘2022-06-12’); Returns 6 as output
YEAR ( )
This function returns the year from a given date value.
Syntax:
Select YEAR(‘date-value’);
Example:
Select YEAR(‘2022-06-12’); Returns 2022 as output
DAYNAME ( )
This function returns the name of the weekday from a given date value.
Syntax:
Select DAYNAME(‘date-value’);
Example:
Select DAYNAME(‘2022-10-23’); Returns Sunday as output
DAYOFMONTH ( )
This function returns the day number (an integer value in the range 1 to 31) of the month specified
in the date value.
Syntax:
Select DAYOFMONTH(‘date_value’);
Example:
Select DAYOFMONTH(‘2022-02-20’); Returns 20 as output
DAYOFWEEK ( )
This function returns the day number (an integer value in the range 1 to 7) of the week specified
in the date value.
Syntax:
Select DAYOFWEEK(‘date_value’);
Example:
Select DAYOFWEEK(‘2022-10-24’); Returns 2 as output

24
DAYOFYEAR ( )
This function returns the day number (an integer value in the range 1 to 366) of the year specified
in the date value.
Syntax:
Select DAYOFYEAR(‘date_value’);
Example:
Select DAYOFYEAR(‘2022-02-20’); Returns 51 as output
NOW ( )
This function returns the current date and time in the format YYYY-MM-DD HH:MM:SS. The time
returned by NOW( ) is a constant time which indicates at which time the statement began to
execute.
Example:
Select NOW( ); Returns 2022-10-25 21:51:13 as output
SYSDATE ( )
This function returns the date and time of the system in the format YYYY-MM-DD HH:MM:SS.
Here, the output of SYSDATE( ) looks similar to NOW( ) function but internally there is a little
difference – the time displayed in the output of SYSDATE( ) function is the system time at which
the function executes.
NOW( ) halts (pauses) the execution but SYSDATE( ) does not halt
Let’s consider the example –
select NOW( ), sleep(10), NOW( );
output : '2022-10-25 22:07:16', '0', '2022-10-25 22:07:16'
select SYSDATE( ), sleep(10), SYSDATE( );
output : '2022-10-25 22:08:18', '0', '2022-10-25 22:08:28'

Multi Row / Group / Aggregate Functions


The multi row functions work on a group of rows / a range of data values and return one result
for every group. In short cut we can say multi row functions process multiple records. These are
also called as group functions / aggregate functions.
The group functions supported in MySql are given below –
MAX ( )
This function returns the maximum value from a given column or expression.
Example:
select MAX(TOTAL_MARKS) as Highest_Mark from STUDENT_MARK;
Example:
select MAX(TOTAL_MARKS) as ETC_TOPPER from student_mark where BRANCH='Electronics';
MIN ( )
This function returns the minimum value from a given column or expression.
Example:
select MIN(TOTAL_MARKS) as Lowest_Mark from STUDENT_MARK;
Example:

25
select MIN(TOTAL_MARKS) as CS_LOWEST from student_mark where BRANCH='Computer
Science';
SUM ( )
This function returns the sum total of values under a specified column.
Example:
select SUM(TOTAL_MARKS) from STUDENT_MARK;
Example:
select SUM(TOTAL_MARKS) from STUDENT_MARK where BRANCH=’Mechanical;
AVG ( )
This function returns the average of values under a specified column or expression.
Example:
select AVG(TOTAL_MARKS) from STUDENT_MARK;
Example:
select AVG(TOTAL_MARKS) from STUDENT_MARK where BRANCH=’Electrical’;
COUNT ( )
This function is used to count total no of records present in a table / to count total no of values
present under a column etc. This function can be used in 3 different ways –
i. COUNT(*): Displays total no of records present in a table.
Select COUNT(*) from STUDENT_MARK;
ii. COUNT(col_name): Displays total no of values under the specified column name. It ignores
null values.
Select COUNT(BRANCH) from STUDENT_MARK;
iii. COUNT(DISTINCT col_name): Displays distinct no of values (repeated values are considered
as one) under the specified column name.
Select COUNT(DISTINCT BRANCH) from STUDENT_MARK;

26

You might also like