MySQL Functions
MySQL Functions
•Definition:
• A function is a special types
of command that performs
some operation and returns a
single value as a result.
• Functions operate on zero,
one , two or more values
provided to them .
(parameters or arguments)
Working with • All the functions
are categorized into
Functions following two types
CONCAT Example:
1. Select concat(“CHIREC”, “SCHOOL”)
2. Select concat(fname, lname) as name from
students;
This function converts a string into lowercase
Syntax: Lower(str);
LOWER/ Lcase(str);
LCASE Example:
select lower(“Rahul”);
select lcase(“RamyA”);
This function converts a string into Uppercase
Syntax: upper(str);
UPPER/ ucase(str);
UCASE Example:
select upper(“Ramya”);
select ucase(“rahul”);
This function extracts
a substring from a
given string
substr(str, m [, n])
substr(str from m [for n])
Returns a portion of str, beginning at character m,n
characters long
SUBSTR 1. If m is positive, MySQL counts from the beginning of
the string to find the first character
2. If m is negative, MySQL counts backwards from the end
of string .
3. If m is 0, it is treated as 1
4. If n is omitted, MySQL returns all characters to the end
of string .
5. If n is less than 1, a null is returned
• SELECT SUBSTRING('MySQL
SUBSTRING',1,5); result : MySQL
• SELECT SUBSTRING('MYSQL
SUBSTRING',7); result : SUBSTRING
• SELECT SUBSTRING('MYSQL SUBSTRING',
Examples 0); result : empty string
• SELECT SUBSTRING('MySQL SUBSTRING',-
15,5); result: MYSQL
• SELECT SUBSTRING('MySQL SUBSTRING',-
10); result : SUBSTRING
• SELECT SUBSTRING('MYSQL SUBSTRING',
3,-1); result: empty string (null) because n is -1
• This function removes leading spaces ie., spaces from
the left of given string.
Syntax:
LTRIM (str);
Example:
Select ltrim(‘ abcdef rdbms Mysql ‘)
LTRIM
• This function removes trailing spaces
i.e., spaces from the right of the given
string.
Syntax:
RTRIM RTRIM (str);
Example:
Select rtrim(‘ abcdef rdbms
Mysql ‘)
• This function removes leading and trailing spaces from the string.
Syntax: TRIM([{BOTH | LEADING | TRAILING} [remstr]
FROM] str)
Returns a string str with all remstr prefixes or suffixed removed
Example:
• Select trim(‘ abcdef rdbms Mysql ‘)
• Select trim (leading ‘a ‘ from ‘ abcdef ‘);
SYSDATE() Returns the current date & Time as YYYY- Select NOW();
MM-DD HH:MM:SS 2010-10-02 11:30:02
SYSDATE() Returns the current date & Time as YYYY- Select SYSDATE();
MM-DD HH:MM:SS 2010-10-02 11:30:10
DATE() Returns the date part of a date time Select DATE(SYSDATE());
expression. 2010-10-02
MONTH() / Returns the Month/Year from given date Select MONTH(CURDATE());
YEAR() argument. 10
Parent Table
Child Table
Implementing Foreign Key Cont..
Parent table
CREATE TABLE Department should be
created
( DeptNo char(2) NOT NULL PRIMARY KEY,
first.
DeptName char(10) NOT NULL, Head
char(30) );