Mysql - Functions

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 10

MySQL

Functions
String Functions
Char() : This function returns the ASCII character
of each integer passed.
E.g., select char(65,66,67,68);
ABCD
concat() : This function concatenates values
from two columns or two strings .
E.g1., select concat(name,aggregate) as “Name
Marks” from student;
E.g2., select concat(name,” is a “),rank) from
faculty;
LOWER() : This function will convert a string into
lower.
E.g1., select lower(‘WELCOME’);
Output : welcome.
E.g2., select
lower(‘MR.OBAMA’),lower(‘MRS.GANDHI’);
SUBSTR() : This function extracts a substring from a given
string.
Syntax: substr(str,m,n);
• Returns a portion of str, beginning at character m and n
characters long.
• If m is 0, it is treated as 1.
• If m is positive, MySQL counts from the beginning of str
to find the find the first character.
• If m is negative, it counts backwards from the end of str.
E.g1., select substr(‘ABCDEFG’,3,4)  CDEF.
E.g2., select substr(‘ABCDEFG’,-3,2)  EF.
Numeric Functions
MOD() : This function returns modulus of given two
numbers.
Syntax : select mod(m,n); Returns remainder of argument
m divided by argument n.
E.g., select mod(11,4)  3
POWER/POW() : This function returns a number m raised
to the power n.
Syntax : select pow(m,n); Returns mn
E.g., select pow(3,4)  34 = 81.
ROUND() : This function returns a number rounded off as
per given specifications.
E.g., select round(15.193,1)  15.2
Select round(23.4588,3)  23.459
Select rount(15.193,-1) 20 (round off to the next nearest
multiple of 10)
SQRT() : This function returns the square root of the given
number.
select sqrt(25)  5
Select sqrt(50) 7.0710678
select pow(3,4)  34 = 81.
TRUNCATE() : This function returns a number with some
digits truncated.
E.g., select truncate(15.193,1)  15.1
Select round(23.4588,3)  23.458
Select round(15.193,-1)  10 (round off to the previous
nearest multiple of 10.
DATE/TIME Functions
CURDATE() : This function returns modulus of given two
numbers.
Select curdate()  current date in yyyy-mm-dd format will
be displayed.
Select curdate()+10;  10 days after the current day will
be displayed, but in yyyymmdd format.
Select sysdate() It will display the current date and time.
Select now() This also will display the current date and
time.
DATE() : This function returns the date part of a date.
E.g1.,Select date(‘2008-12-31 01:02:03’)  2008-12-31.
MONTH() : This function returns the month part of the
date passed.
E.g1.,Select month(‘2008-02-03’);  2
YEAR() : This function returns the year part of the date
passed.
E.g1.,Select year(‘2008-02-03’);  2008
DAYNAME() : This function returns the dayname of the day.
Select dayname(‘2018-01-08’);  monday
DAYOFMONTH() : This function returns the day of month.
Select dayofmonth(‘2008-12-31’);  31.
DAYOFYEAR() : This function returns the day of the year of
the date in the range 1 to 366.
Select dayofyear(‘2018-02-10’);  41.
NOW() : This function returns the current data and time in
the following format : yyyy-mm-dd hh:mm:ss
SYSDATE() : This function returns the time at which the
function executes.

You might also like