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

Mysql Functions

Uploaded by

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

Mysql Functions

Uploaded by

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

REVISION OF MYSQL FUNCTIONS

FUNCTIONS IN MYSQL

A function can be defined as a set of predefined commands when called performs certain computation &
return a value.

Function in MySQL are classified in two types:


 Single Row functions
 Multiple Row functions

Single Row functions :

 Single row functions operate on a single value and return a single value.
 When applied to a table, they return a single result for every row of the queried table.

They are classified into :

 String functions
 Numeric functions
 Date & time functions

Multiple Row functions :

 These are also called aggregate functions


 Aggregate functions is used to perform calculation on group of rows and return the calculated
summary.

AGGREGATE FUNCTIONS

Function Description

MAX() Returns the maximum/highest value among the values in the given
column/expression.
MIN() Returns the minimum/lowest value among the values in the given column/expression.
SUM() Returns the sum of the values under the specified column/expression.
AVG() Returns the average of the values under the specified column/expression.
COUNT(*) Returns the total number of records in a table.
COUNT(column_name) Returns the total number of values in column.

MATH FUNCTIONS

 POWER(a,b) Returns the a raise to the power b.


Syntax : POW(M,N )
Example : SELECT POW ( 3 , 2) ; Output : 9
Example : SELECT POW (4 , - 2) ; Output : 0.0625
 ROUND(n,d) Returns the n rounded to d places after decimal.
Syntax : ROUND(N,[D])

Example : SELECT ROUND(4.43) ; Output : 4

Example : SELECT ROUND(- 4.53) ; Output : - 5

Example : SELECT ROUND(- 4.535,2) ; Output : - 4.54

 MOD(a,b) Returns remainder after integer division.


Syntax : MOD(N,M), N % M, N MOD M;

Example : SELECT MOD (17,5) ; Output : 2

Example : SELECT MOD (17 MOD 5) ; Output : 2


STRING FUNCTIONS
Syntax : LCASE(str) Example : SELECT LCASE('MYTESTSTRING'); Output : myteststring
Syntax : UPPER(str) Example : SELECT UPPER('myteststring'); Output : MYTESTSTRING
MID() extracts a substring from a string
Syntax : MID(str,pos,len) Example : SELECT MID('w3resource',4,3); Output : eso
Syntax : LENGTH(str) Example : SELECT LENGTH('text'); Output : 4
Syntax : LEFT(str,len) Example : SELECT LEFT('w3resource', 3); Output : w3r
Syntax : RIGHT(str,len) Example : SELECT RIGHT('w3resource',8); Output : resource
Syntax : INSTR(str,substr) Example : SELECT INSTR('myteststring','st'); Output : 5
Syntax : LTRIM(str) Example : SELECT LTRIM(' Hello') Output : Hello
Syntax : RTRIM(str) Example : SELECT RTRIM('w3resource '); Output : w3resource
Syntax : TRIM([{BOTH | LEADING | TRAILING} [remstr] FROM] str)
Example : SELECT TRIM(' trim '); # (leading and trailing space removed) Output : trim
Example : SELECT TRIM(LEADING 'leading' FROM 'leading text' ); Output : text
Example : SELECT TRIM(BOTH 'lead trail' FROM 'lead trail text lead trail'); Output : text
MYSQL Date Functions:
NOW() returns the value of current date and time in ‘YYYY-MM-DD HH: MM:SS’ format
SELECT NOW(); Output :

NOW()
-------------------
2014-08-08 10:04:25

DATE() takes the DATE part out from a DATEtime expression.


SELECT DATE('2008-05-17 11:31:31') as required_DATE; Output :
required_DATE
-------------------
2008-05-17

MONTH() returns the MONTH for the date within a range of 1 to 12 ( January to December)
SELECT MONTH('2009-05-18'); Output :
MONTH('2009-05-18')
------------------- -----
5
MONTHNAME() returns the full name of the month for a given date.
SELECT MONTHNAME('2009-05-18'); Output :
MONTHNAME('2009-05-18')
--------------------------------
May
DAY() returns the day of the month for a specified date.
SELECT DAY('2008-05-15'); Output :
DAY('2008-05-15')
-------------------
15
DAYNAME() returns the name of the week day of a date,.
SELECT DAYNAME('2008-05-15'); Output :
DAYNAME('2008-05-15')
---------------------------
Thursday
YEAR() returns the year for a given date.
SELECT YEAR('2009-05-19'); Output :
YEAR('2009-05-19')
---------------------
2009
Examples:
1) mysql>select round(2.25);

+---------------+
| round(2.25) |
+---------------+
|2|
+---------------+
1 row in set (0.01 sec)
2) mysql>select round(2.25, 1);

+------------------+
| round(2.25, 1) |
+------------------+
| 2.3 |
+------------------+
1 row in set (0.00 sec)
3) mysql>select round(2.25, 2);

+-------------------+
| round(2.25, 2) |
+------------------+
| 2.25 |
+------------------+
1 row in set (0.00 sec)
4) mysql>select round(2.26, 0);

+------------------+
| round(2.26, 0) |
+------------------+
|2|
+------------------+
1 row in set (0.00 sec)
5) mysql>select round(135.43, 0);

+----------------------+
| round(135.43, 0) |
+---------------------+
| 135 |
+---------------------+
1 row in set (0.00 sec)

6) mysql>select round(135.53, 0);

+----------------------+
| round(135.53, 0) |
+----------------------+
| 136 |
+----------------------+
1 row in set (0.00 sec)
7) mysql>select round(135.55, 1);

+----------------------+
| round(135.55, 1) |
+----------------------+
| 135.6 |
+----------------------+
1 row in set (0.00 sec)
8) mysql>select round(135.55, -1);

+-----------------------+
| round(135.55, -1) |
+-----------------------+
| 140 |
+-----------------------+
1 row in set (0.00 sec)
9) mysql>select round(134.45, -1);

+------------------------+
| round(134.45, -1) |
+-----------------------+
| 130 |
+-----------------------+
1 row in set (0.00 sec)
10) mysql>select round(134.45, -2);

+------------------------+
| round(134.45, -2) |
+-----------------------+
| 100 |
+-----------------------+
1 row in set (0.00 sec)

11) mysql>select round(154.45, -2);

+------------------------+
| round(154.45, -2) |
+-----------------------+
| 200 |
+-----------------------+
1 row in set (0.00 sec)
12) mysql>select round(1454.45, -2);

+------------------------+
| round(1454.45, -2) |
+------------------------+
| 1500 |
+------------------------+
1 row in set (0.00 sec)
13) mysql>select round(1444.45, -2);

+-------------------------+
| round(1444.45, -2) |
+------------------------+
| 1400 |
+------------------------+
1 row in set (0.00 sec)
14) mysql>select round(1444.45, -3);

+-------------------------+
| round(1444.45, -3) |
+-------------------------+
| 1000 |
+------------------------+
1 row in set (0.00 sec)
15) mysql>select round(1544.45, -3);

+-------------------------+
| round(1544.45, -3) |
+-------------------------+
| 2000 |
+-------------------------+
1 row in set (0.00 sec)

You might also like