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

Chapter 10 SQL Functions

This document provides an overview of MySQL functions. It discusses numeric, string, date and time, and aggregate functions. Numeric functions perform calculations and return numeric values. String functions manipulate string values. Date and time functions return date/time values or parts of a date/time. Aggregate functions operate on sets of rows and return a single value. The document provides examples of commonly used functions from each category.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

Chapter 10 SQL Functions

This document provides an overview of MySQL functions. It discusses numeric, string, date and time, and aggregate functions. Numeric functions perform calculations and return numeric values. String functions manipulate string values. Date and time functions return date/time values or parts of a date/time. Aggregate functions operate on sets of rows and return a single value. The document provides examples of commonly used functions from each category.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Chapter 10:

MySQL – Functions

Informatics Practices Revised as per


CBSE
Class XI (CBSE Board) Curriculum
2015

Visit www.ip4you.blogspot.com for more….

Authored By:- Rajesh Kumar Mishra, PGT (Comp.Sc.)


Kendriya Vidyalaya Upper Camp, Dehradun (Uttarakhand)
e-mail : rkmalld@gmail.com
Objective
In this presentation, you will learn about-
 Introduction to MySQL Functions.
 Types of MySQL Functions
 Numeric Functions
 String Functions
 Date & Time Functions
 Aggregate Functions
Working with Functions
 What is Function?
A function is a special types of command in MySQL that
performs some operation on table and returns a single value
as a result.
 Types of Functions:
 Numeric Functions
 String Functions
 Date & Time Function
 Aggregate Functions

 Numeric, String and Date-Time functions are called Single row


functions because they can accept one row and return only one value.
When applied on a table, they return a single result for every row of
the queried table.
 Aggregate Functions are called Multiple row functions because they
operate on a set of rows to return a single value.
Numeric Functions

These functions may accept some numeric values and performs


required operation, returns numeric values as result.
Name Purpose Example
MOD (M, N) Returns remainder of M Select MOD(11,4) ;
divide by N 3
POWER (M, N) Returns MN Select POWER(3,2);
POW (M, N) 9
ROUND (N [,M]) Returns a number rounded Select ROUND(15.193,1);
off up to M place. If M is -1, 15.2
it rounds nearest 10. Select ROUND(15.193);
If M is not given, then N is 15
rounded to the nearest
Integer. Select ROUND(-1.58);
-2
SQRT (N) Returns square root of N Select SQRT(25);  5

TRUNCATE(N,M) Returns number after Select TRUNCATE(15.79,1)


truncating M decimal place.  15.7
String Functions
 CONCAT()
Concatenates (Adds) two string.
CONCAT(Str1 , Str2)
mysql> SELECT CONCAT(‘ab’ , ‘cd’ ) FROM DUAL;
 abcd
mysql> SELECT CONCAT(‘Mr’, Name) FROM Student;
 Concat() can be nested.
mysql> SELECT CONCAT(CONCAT(Name,’son of ‘), Fname)
FROM Student;
 LENGTH()
Returns length of given string.
LENGTH (Str)
mysql> SELECT LENGTH(‘abcd’ ) FROM DUAL;
4
mysql> SELECT Name, LENGTH(Name) FROM Student;
String Functions cont…
 LOWER() or LCASE()
Converts given string in lower case.
LOWER (Str)
mysql> SELECT LOWER(‘ABcD’ ) FROM DUAL;
 abcd
mysql> SELECT LOWER(Name) FROM Student;
mysql> SELECT LCASE(Fname) FROM Student;

 UPPER() or UCASE()
Converts given string in upper case.
UPPER (Str)
mysql> SELECT UPPER(‘abcD’ ) FROM DUAL;
 ABCD
mysql> SELECT UPPER(Name) FROM Student;
mysql> SELECT UCASE(Fname) FROM Student;
String Functions cont…

 LTRIM()
Returns string after removing leading spaces.
mysql> SELECT LTRIM(‘ abcd’ ) FROM DUAL;
 abcd
mysql> SELECT LTRIM(Name) FROM Student;

 RTRIM()
Returns string after removing trailing spaces.
mysql> SELECT RTRIM(‘abcd ’ ) FROM DUAL;
 abcd
mysql> SELECT RTRIM(Name) FROM Student;
 TRIM()
Returns string after removing leading and trailing spaces.
mysql> SELECT TRIM(‘ abcd ’ ) FROM DUAL;
 abcd
String Functions cont…
 SUBSTR()
Returns a sub string of given length from specified position.
SUBSTR (Str, position [,length])
mysql> SELECT SUBSTR(‘MY COMPUTER’, 4,3’ )  COM
 If position is negative then backward position is counted.
mysql> SELECT SUBSTR(‘ABCDEFG’ , -5, 4) FROM Student;
 CDEF
 If Length is omitted then up to end of the string is considered.
mysql> SELECT SUBSTR(‘ABCDEFG’ , 3) FROM Student;
 CDEFG
 INSTR()
Searches a string in to another string and returns its position.
INSTR(Str1, Str2)
mysql> SELECT INSTR(‘CORPORATE FLOOR’, ‘OR’);  2
mysql> SELECT Name, INSTR(Name,’a’) FROM Student;
String Functions cont…
 LEFT()
Returns leftmost string up to given length.
LEFT (Str , length)
mysql> SELECT LEFT(‘MYSQL’, 2 )  MY
mysql> SELECT LEFT( Name, 4) FROM Student;
 RIGHT()
Returns rightmost string up to given length.
RIGHT (Str , length)
mysql> SELECT RIGHT(‘MYSQL’, 3 )  SQL
mysql> SELECT RIGHT (Name, 4) FROM Student;
 MID()
Returns a substring upto given length from given position.
MID (Str ,Pos, Length) Mid() is
mysql> SELECT MID(‘COMPUTER’, 4,3 )  PUT similar to
Substr()
mysql> SELECT MID (Name, 4,3) FROM Student;
Summery of String Functions
Name Purpose Example
CONCAT(str1,str2) Returns concatenated string i.e. Select CONCAT(Name, City)
str1+str2. from Student;
LOWER(str) / Returns the given string in lower Select LOWER(‘ABC’);  abc
LCASE(str) case.

UPPER(str) / Returns the given String in upper Select UPPER(‘abc’);  ABC


UCASE(str) case.

LTRIM(str) Removes Leading/Trailing/both Select TRIM(‘ ABC ‘);


RTRIM(str) spaces from given string.  ‘ABC’
TRIM(str)
LEFT(str, N) Returns the (N) characters from Select LEFT(‘Computer’,4);
RIGHT(str,N) left/right from the given string.  Comp

SUBSTR(str,P,[N]) / Returns the substring for given Select


MID (str,P,N) position(P) and length (N). If M is (- SUBSTR(‘Computer’,3,2);
ve) then backward position counted.  mp
INSTR(str1,str2) Returns the index of first occurrence Select INSTR(‘Common’, ’m’);
of str2 in str1. 3
LENGTH(str) Returns the length of given string Select LENGTH(‘Common’);
6
Date & Time Functions
 CURDATE() or CURRENT_DATE()
Returns current date of the system in YYYY-MM-DD format.
mysql> SELECT CURDATE() ;  2014-01-30
mysql> SELECT CURDATE()+10 ;  2014-02-09

 SYSDATE()
Returns current date and time as YYYY-MM-DD HH:MM:SS
mysql> SELECT SYSDATE() ;
 2014-01-30 10:30:20
 NOW()
Returns current date and time as YYYY-MM-DD HH:MM:SS
mysql> SELECT SYSDATE() FROM DUAL
 2010-01-30 10:30:20

Difference between SYSDATE() & NOW()


NOW() returns the time when command began to execute and does not change time
during execution. Where as SYSDATE() changes its time continuously.
Date & Time Functions cont…
 DATE()
Returns date part of the given date-time expression.
DATE (Dt)
mysql> SELECT DATE(‘2008-12-31 01:02:03’) ;
 2008-12-32
mysql> SELECT DATE( SYSDATE());
 YEAR()
Returns year of the given date expression.
YEAR (Dt)
mysql> SELECT YEAR(‘2008-12-31’) ;  2008
mysql> SELECT YAER(DOB) FROM Student;

 MONTH()
Returns month of the given date expression.
MONTH (Dt)
mysql> SELECT MONTH(‘2008-12-31’) ;  12
mysql> SELECT MONTH( CURDATE());
Date & Time Functions cont…

 DAYOFMONTH()
Returns day of month of the given date expression.
DAYOFMONTH (Dt)
mysql> SELECT DAYOFMONTH(‘2008-12-31’) ;
 31
mysql> SELECT DAYOFMONTH( CURDATE()) ;
mysql> SELECT DAYOFMONTH( DOB) FROM Student;

 DAYNAME()
Returns the name of Week day of the given date expression.
DAYNAME (Dt)
mysql> SELECT DAYNAME(‘2008-12-31’) ;
 SUNDAY
mysql> SELECT DAYNAME( CURDATE()) ;
mysql> SELECT DAYNAME( DOB) FROM Student;
Date & Time Functions cont…

 DAYOFWEEK()
Returns day of week i.e. 1- Sunday, 2- Tuesday.. etc. of given
date.
DAYOFWEEK (Dt)
mysql> SELECT DAYOFWEEK(‘2008-12-31’) ;
1
mysql> SELECT DAYOFWEEK(CURDATE()) ;
 DAYOFYEAR()
Returns the day of year of the given date expression.
DAYOFYAER (Dt)
mysql> SELECT DAYOFYAER(‘2010-02-05’) ;
 36
mysql> SELECT DAYOFYAER( CURDATE()) ;
mysql> SELECT DAYOFYEAR( DOB) FROM Student;
Summery of Date & Time Functions
Name Purpose Example
CURDATE() / Returns the current date in Select CURDATE();
CURRENT_DATE() YYYY-MM-DD format.  2013-10-02

NOW() Returns the current date & Time Select NOW();


as YYYY-MM-DD HH:MM:SS  2013-10-02 11:30:02

SYSDATE() Returns the current date & Time Select SYSDATE();


as YYYY-MM-DD HH:MM:SS  2013-10-02 11:30:10

DATE() Returns the date part of a date- Select DATE(SYSDATE());


time expression.  2013-10-02

MONTH() Returns the Month/Year from Select MONTH(‘2012-10-02’);


YEAR() given date argument.  10

DAYNAME() Returns the name of the Select DAYNAME(CURDATE());


weekday  SUNDAY

DAYOFMONTH() Returns the day of month Select


(1-31). DAYOFMONTH(CURDATE());

DAYOFWEEK() Returns the day of week (1-7). Select DAYOFWEEK(CURDATE());

DAYOFYEAR() Returns the day of year(1-366). Select DAYOFYEAR(CURDATE());


Aggregate Functions
 SUM()
Returns sum of given column in the table.
SUM (<Field>)
mysql> SELECT SUM (Sal) FROM Emp;
mysql> SELECT SUM(Sal) FROM Emo WHERE City=‘Jaipur’;
 MIN()
Returns minimum value in the given column of table.
MIN (<Field>)
mysql> SELECT MIN (Sal) FROM Emp;
mysql> SELECT MIN(Sal) FROM Emp WHERE City=‘Jaipur’;
 MAX()
Returns maximum value in the given column of table.
MAX (<Field>)
mysql> SELECT MAX (Sal) FROM Emp;
mysql> SELECT MAX(Sal) FROM Emp WHERE City=‘Jaipur’;
Aggregate Functions
 AVG()
Returns average value of given column in the table.
AVG (<Field>)
mysql> SELECT AVG (Sal) FROM Emp;
mysql> SELECT AVG(Sal) FROM Emo WHERE City=‘Jaipur’;
 COUNT()
Returns number of values in the given column of table. It also
reflect the number of record in the table.
COUNT (<Field|*>)
mysql> SELECT COUNT (Name) FROM Emp;
mysql> SELECT COUNT(Name) FROM Emp
WHERE City=‘Jaipur’;
mysql> SELECT COUNT (*) FROM Emp;
 Number of records in the Emp table
mysql> SELECT COUNT(*) FROM Emp
WHERE City=‘Jaipur’;
Aggregate Functions

Name Purpose Example


SUM() Returns the sum of given Select SUM(Pay) from Emp;
column. Select Sum(Pay), Sum(Net)
from Emp;
MIN() Returns the minimum value Select MIN(Pay) from Emp;
in the given column.
MAX() Returns the maximum value Select MAX(Pay) from Emp;
in the given column.
AVG() Returns the Average value of Select AVG(Pay) from Emp;
the given column.
COUNT() Returns the total number of Select COUNT(Name) from Emp;
values/ records in given Select COUNT(*) from Emp;
column.

Aggregate Functions should not be used with other columns which


may have multiple values in the table. The following query is illogical
and wrong. Why? Think yourself….
Select sum(pay), name from Employee;

You might also like