SlideShare a Scribd company logo
STRUCTURED QUERY
LANGUAGE
FUNCTIONS IN MYSQL
FUNCTIONS IN MYSQL
There are two types of functions in MySQL:
Single Row Functions : Single row functions operate on a single value to
return a single value. They return only one result per row. They are further
categorized into:
Numeric functions
String functions Date
Time functions
Multiple Row Functions: Multiple row functions operate on a set of rows to
return a single value. Examples include SUM(), AVG() and COUNT().
TABLE: ITEM
Consider the table ITEM:
Itemno Name Price Quantity DOP
A001 Bread 20.25 10 2015-05-21
A002 Butter 50.75 6 2015-06-12
A003 Biscuits 10.32 20 2015-12-23
A004 Eggs 70.75 10 2015-09-18
A005 Chocolate 100.35 20 2015-10-19
NUMERIC FUNCIONS
Numeric functions perform operations on numeric values and return numeric values. There are
three numeric functions which are available:
POWER(X,Y) OR POW(X,Y): It returns the value of X raised to the power of Y.
COMMAND OUTPUT EXPLANATION
SELECT POW(2,3) ; 8 2 X 2 X 2 =8
SELECT POW(2, -2); 0.25 1/22 = ¼ = 0.25
SELECT POW(-2,2); 4 -2 X -2 = 4
SELECT POW (-2,3); -8 -2 X -2 X -2 = -8
SELECT ITEMNO,
POW(PRICE,2) FROM ITEM;
ROUND FUNCTION
 ROUND(X,D) function is used to round the value of argument X upto D decimal places.
 If number of decimal places is not specified or is zero, the number rounds to the nearest integer OR 0
decimal places.
 If negative value is specified for precision, it counts off that value left from the decimal point. If positive
value is specified for precision, it counts off that value right from the decimal point.
COMMAND RESULT EXPLNATION
SELECT ROUND(-4.45); -4 No. after decimal is less than 5
SELECT ROUND(-4.67); -5 No. after decimal is more than 5
SELECT ROUND(4.45); 4 No. after decimal is less than 5
SELECT ROUND(4.678,1); 4.7 No. at second place after decimal is more than 5
SELECT ROUND(4.678,0); 5 No. after decimal is more than 5
SELECT ROUND(56.345, -1); 60
SELECT ITEMNO, ROUND(PRICE) FROM
ITEM;
TRUNCATE FUNCTION
 TRUNCATE(X,D) or TRUNC(X,D) returns the number X, truncated to D decimal places.
 If D is 0, the result has no decimal point or fractional part.
 If D is negative, it causes D digits left of the decimal point of the value X to become zero.
COMMAND OUTPUT
SELECT TRUNC(5.678,0); 5
SELECT TRUNC(3.768,1); 3.7
SELECT TRUNC(-9.87,1); -9.8
SELECT TRUNC(345, -2); 300
SELECT ITEMNO,TRUNC(PRICE,1)
ITEM;
STRING(CHARACTER) FUNCTION
String functions operate on character type data. They return either character or numeric
values.
LENGTH(str): Returns the length of the string str.
COMMAND OUTPUT
SELECT LENGTH(“COMPUTER”); 8
SELECT LENGTH(“ I AM LEARNING”); 13
SELECT ITEMNO, LENGTH(NAME) FROM
ITEM;
CONCAT FUNCTION
CONCAT(str1, str2,...) : Returns the string that results from concatenating the arguments.
COMMAND OUTPUT
SELECT CONCAT(“My”,
“SQL”, “CLASS”);
MySQLCLASS
SELECT CONCAT(“My”,
“SQL”,NULL, “CLASS”);
NULL
SELECT CONCAT(ITEMNO,
NAME), PRICE FROM ITEM;
CONCAT(Itemno, Name)
A001Bread
A002Butter
A003Biscuits
A004Eggs
A005Chocolate
INSTR FUNCTION
INSTR(str, substr) : Returns the position of the first occurrence of substring substr in
string str
COMMAND RESUL
T
EXPLANATION
SELECT INSTR(“My SQL”,
“SQL);
4 First match found
at 4th position
SELECT INSTR(“My SQL”,
“SQR”);
0 No match found
SELECT INSTR(NAME,
“BREAD”) FROM ITEM;
INSTR(NAME,”BREAD
”)
1
0
0
0
0
LOWER FUNCTION
LOWER(str) or LCASE(str): Returns the string str in lowercase.
COMMAND RESULT
SELECT
LOWER(“INFORMATICS”)
informatics
SELECT LOWER(NAME)
FROM EMP;
LOWER(Name)
bread
butter
biscuits
eggs
chocolate
UPPER FUNCTION
UPPER(str) or UCASE(str): Returns the string str in lowercase.
COMMAND RESULT
SELECT
UPPER(“INforMAtiCS”);
INFORMATICS
SELECT UPPER(NAME)
FROM EMP;
UPPER(Name)
BREAD
BUTTER
BISCUITS
EGGS
CHOCOLATE
LEFT FUNCTION
LEFT(str, N) : Returns the N characters from the left side of the string str.
COMMAND RESULT
SELECT LEFT(“My SQL”, 2); My
SELECT LEFT(NAME, 3) FROM ITEM;
LEFT(Name, 3)
Bre
But
Bis
Egg
Cho
RIGHT FUNCTION
RIGHT(str, N) : Returns the N characters from the right side of the string str.
COMMAND RESULT
SELECT RIGHT(“My SQL”, 3); SQL
SELECT RIGHT(NAME, 3) FROM ITEM;
RIGHT(Name)
ead
ter
its
ggs
ate
LTRIM FUNCTION
LTRIM(str) : Returns the leading spaces ie from the left side of the string str.
COMMAND RESULT
SELECT LTRIM(“
INFORMATICS”);
INFORMATICS
SELECT LTRIM(NAME)
FROM EMP;
LTRIM(Name)
Bread
Butter
Biscuits
Eggs
Chocolate
RTRIM FUNCTION
RTRIM(str) : Removes the trailing spaces ie from the right side of the string str.
COMMAND RESULT
SELECT RTRIM(“INFORMATICS
”);
INFORMATICS
SELECT RTRIM(NAME) FROM
EMP;
RTRIM(Name)
Bread
Butter
Biscuits
Eggs
Chocolate
TRIM FUNCTION
TRIM(str) : Removes both leading and trailing spaces from the string str .
COMMAND OUTPUT
SELECT TRIM(“ INFORMATICS
PRACTICES ”);
INFORMATICS
SELECT TRIM(NAME) FROM EMP;
TRIM(Name)
Bread
Butter
Biscuits
Eggs
Chocolate
SUBSTRING/SUBSTR/MID FUNCTION
SUBSTRING(str, M,N) or MID(str, M,N): Returns the specified number of characters from
the middle of the string. There are 3 arguments.
 The first argument is the source string.
 The second argument is the position of first character to be displayed.
 The third argument is the number of characters to be displayed.
If the third argument is missing, then starting from the position specified, the rest of the
string is returned.
If the second argument is negative, then the beginning of the substring is M characters
from the end of the string.
SUBSTRING/SUBSTR/MID FUNCTION Contd…
COMMAND OUTPUT
SELECT SUBSTR(“WORLD OF COMPUTERS”, 4 ); or
SELECT SUBSTR(“WORLD OF COMPUTERS” FROM 4 );
LD OF COMPUTERS
SELECT SUBSTR(“WORLD OF COMPUTERS” , 3, 4 ); LD O
SELECT SUBSTR(“WORLD OF COMPUTERS”, -4 ); TERS
SELECT SUBSTR(“WORLD OF COMPUTERS”, -6,3 ); PUT
SELECT SUBSTR(“WORLD OF COMPUTERS” FROM -4
FOR 2 );
TE
SELECT SUBSTR(NAME,2,3) FROM ITEM;
SUBSTR(Nam
e,2,3)
rea
utt
isc
ggs
hoc
ASCII FUNCTION
ASCII(str): Returns the ASCII value of the leftmost character of the string str.
 Returns 0 if str is an empty string.
 Returns NULL if str is NULL.
COMMAND RESUL
T
EXPLANATION
SELECT ASCII(‘2’); 50 ASCII value of ‘0’ is 48, ‘1’ is 49 , ‘2’ is 50 and so on
SELECT ASCII(‘CG’); 67 ASCII value of ‘A’ is 65, ‘B’ is 66, ‘C’ is 67 and so on
SELECT ASCII(‘are’); 97 ASCII value of ‘a’ is 97, ‘b’ is 98 and so on
DATE AND TIME FUNCTION
COMMAND EXPLANATION EXAMPLE RESULT
CURDATE() Returns the current date in YYYY-
MM-DD format
SELECT CURDATE(); 2015-10-14
NOW() Returns the current date and time in
'YYYY-MM-DD HH:MM:SS'
SELECT NOW(); 2015-10-14 20:10:30
SYSDATE() Returns the current date and time in
'YYYY-MM-DD HH:MM:SS
SELECT SYSDATE(); 2015-10-14 20:10:30
DATE(exp) Extracts the date part of a date or
date time expression
SELECT DATE(‘2015-10-14
20:10:30’);
2015-10-14
MONTH(date) Returns the numeric month from the
date passed, in the range 0 to 12
SELECT MONTH(‘2015-10-
14’);
SELECT MONTH(DOP)
FROM ITEM;
10
DATE AND TIME FUNCTION Contd……
COMMAND EXPLANATION EXAMPLE RESULT
YEAR(date) Returns the year for date passed in
the range 0 to 9999
SELECT YEAR (‘2015-10-14’);
SELECT YEAR(DAP) FROM ITEM;
2015
DAYNAME(da
te)
returns the name of the weekday for
the date passed
SELECT DAYNAME(2015-10-14);
SELECT DAYNAME(DAP) FROM ITEM;
MONDAY
DAYOFMONT
H(date)
Returns the day of the month in the
range 0 to 31.
SELECT DAYOFMONTH(2015-10-14);
SELECT DAYOFMONTH(DAP) FROM
ITEM;
DAYOFWEEK(
date)
Returns the day of week in number
1 for Sunday, 2 for Monday and so
SELECT DAYOFWEEK(2015-10-14);
SELECT DAYOFWEEK(DAP) FROM
ITEM;
DAYOFYEAR(
ate)
Return the day of the year for the
given date in numeric format in the
range 1 to 366.
SELECT DAYOFYEAR(2015-10-14);
SELECT DAYOFYEAR(DAP) FROM ITEM;
SYSDATE() vs NOW()
SYSDATE() returns the time at which the function executes.
While NOW() which returns a constant time that indicates the
time at which the statement began to execute.

More Related Content

What's hot (20)

PPT
Les09
arnold 7490
 
PPTX
Oracle: Functions
DataminingTools Inc
 
PPT
Les11
arnold 7490
 
PPT
Select To Order By
Krizia Capacio
 
PPT
Les02
arnold 7490
 
DOCX
Built-in Functions in SQL | Numeric Functions
Raj vardhan
 
PDF
Sql insert statement
Vivek Singh
 
PDF
To excel or not?
Filippo Selden
 
PPT
Aggregate functions
sinhacp
 
PPT
Aggregate Functions,Final
mukesh24pandey
 
DOC
Sql functions
G C Reddy Technologies
 
PDF
Alter table command
ravikhandelwal41
 
PPTX
Les05 Aggregating Data Using Group Function
NETsolutions Asia: NSA – Thailand, Sripatum University: SPU
 
PPTX
Interacting with Oracle Database
Chhom Karath
 
PPT
Numeric functions in SQL | Oracle
Vimal Raj
 
Oracle: Functions
DataminingTools Inc
 
Select To Order By
Krizia Capacio
 
Built-in Functions in SQL | Numeric Functions
Raj vardhan
 
Sql insert statement
Vivek Singh
 
To excel or not?
Filippo Selden
 
Aggregate functions
sinhacp
 
Aggregate Functions,Final
mukesh24pandey
 
Sql functions
G C Reddy Technologies
 
Alter table command
ravikhandelwal41
 
Les05 Aggregating Data Using Group Function
NETsolutions Asia: NSA – Thailand, Sripatum University: SPU
 
Interacting with Oracle Database
Chhom Karath
 
Numeric functions in SQL | Oracle
Vimal Raj
 

Viewers also liked (20)

DOCX
Advanced data structures using c++ 3
Shaili Choudhary
 
PPTX
Classes and objects1
Vineeta Garg
 
PPTX
Constructors and destructors
Vineeta Garg
 
PPTX
13. Indexing MTrees - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
PPS
Pp
sheraz1
 
PDF
Discrete Mathematics S. Lipschutz, M. Lipson And V. H. Patil
widespreadpromotion
 
PDF
Working with Cookies in NodeJS
Jay Dihenkar
 
PPTX
Stacks in c++
Vineeta Garg
 
PPTX
Data types in c++
Venkata.Manish Reddy
 
PPTX
16. Algo analysis & Design - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
PPT
C++ data types
pratikborsadiya
 
PPS
Data Structure
sheraz1
 
PPTX
Data file handling in c++
Vineeta Garg
 
PPTX
10. Search Tree - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
PPT
Stacks in algorithems & data structure
faran nawaz
 
PPT
stacks in algorithems and data structure
faran nawaz
 
PPTX
Inheritance in c++
Vineeta Garg
 
PPTX
Queues in C++
Vineeta Garg
 
PPT
Structure of C++ - R.D.Sivakumar
Sivakumar R D .
 
PPTX
11. Hashing - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
Advanced data structures using c++ 3
Shaili Choudhary
 
Classes and objects1
Vineeta Garg
 
Constructors and destructors
Vineeta Garg
 
13. Indexing MTrees - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
Discrete Mathematics S. Lipschutz, M. Lipson And V. H. Patil
widespreadpromotion
 
Working with Cookies in NodeJS
Jay Dihenkar
 
Stacks in c++
Vineeta Garg
 
Data types in c++
Venkata.Manish Reddy
 
16. Algo analysis & Design - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
C++ data types
pratikborsadiya
 
Data Structure
sheraz1
 
Data file handling in c++
Vineeta Garg
 
10. Search Tree - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
Stacks in algorithems & data structure
faran nawaz
 
stacks in algorithems and data structure
faran nawaz
 
Inheritance in c++
Vineeta Garg
 
Queues in C++
Vineeta Garg
 
Structure of C++ - R.D.Sivakumar
Sivakumar R D .
 
11. Hashing - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
Ad

Similar to Structured query language functions (20)

DOCX
Mysql Functions with examples CBSE INDIA 11th class NCERT
Harish Gyanani
 
PPT
Oracle sql ppt2
Madhavendra Dutt
 
PPTX
Math, Text, Date Functions of MySQL for Class-12 CBSE
devsuchaye
 
PDF
sql functions3 (1).pdf
Usha570012
 
PPTX
Built-Functions in MySqll (Advance Database System)
khurtdhangonzales
 
PDF
sql functions3.pdf about the function of sql
mahakgodwani2555
 
PPTX
Unit 3 - Function & Grouping,Joins and Set Operations in ORACLE
DrkhanchanaR
 
PPT
Sql operators & functions 3
Dr. C.V. Suresh Babu
 
PPTX
Functions
Ankit Dubey
 
PPTX
Introduction to sql new
SANTOSH RATH
 
PDF
Mysqlfunctions
N13M
 
PPT
Oracle Sql & PLSQL Complete guide
Raviteja Chowdary Adusumalli
 
PDF
Chapter8 my sql revision tour
KV(AFS) Utarlai, Barmer (Rajasthan)
 
PPT
SQL select statement and functions
Vikas Gupta
 
PDF
SQL BUILT-IN FUNCTION
Arun Sial
 
PPTX
12th.pptx
AamirMaqsood8
 
PPTX
Database Overview
Livares Technologies Pvt Ltd
 
PDF
Bt0075 rdbms with mysql 2
Techglyphs
 
PPT
SQL || overview and detailed information about Sql
gourav kottawar
 
Mysql Functions with examples CBSE INDIA 11th class NCERT
Harish Gyanani
 
Oracle sql ppt2
Madhavendra Dutt
 
Math, Text, Date Functions of MySQL for Class-12 CBSE
devsuchaye
 
sql functions3 (1).pdf
Usha570012
 
Built-Functions in MySqll (Advance Database System)
khurtdhangonzales
 
sql functions3.pdf about the function of sql
mahakgodwani2555
 
Unit 3 - Function & Grouping,Joins and Set Operations in ORACLE
DrkhanchanaR
 
Sql operators & functions 3
Dr. C.V. Suresh Babu
 
Functions
Ankit Dubey
 
Introduction to sql new
SANTOSH RATH
 
Mysqlfunctions
N13M
 
Oracle Sql & PLSQL Complete guide
Raviteja Chowdary Adusumalli
 
Chapter8 my sql revision tour
KV(AFS) Utarlai, Barmer (Rajasthan)
 
SQL select statement and functions
Vikas Gupta
 
SQL BUILT-IN FUNCTION
Arun Sial
 
12th.pptx
AamirMaqsood8
 
Database Overview
Livares Technologies Pvt Ltd
 
Bt0075 rdbms with mysql 2
Techglyphs
 
SQL || overview and detailed information about Sql
gourav kottawar
 
Ad

Recently uploaded (20)

PPTX
Introduction to Indian Writing in English
Trushali Dodiya
 
PDF
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
PDF
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
PPTX
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
PPTX
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
PPTX
TRANSLATIONAL AND ROTATIONAL MOTION.pptx
KIPAIZAGABAWA1
 
PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
PPTX
How to Create a Customer From Website in Odoo 18.pptx
Celine George
 
PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
PPTX
Difference between write and update in odoo 18
Celine George
 
PDF
Android Programming - Basics of Mobile App, App tools and Android Basics
Kavitha P.V
 
PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PDF
Mahidol_Change_Agent_Note_2025-06-27-29_MUSEF
Tassanee Lerksuthirat
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PDF
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
PPTX
PPT-Q1-WEEK-3-SCIENCE-ERevised Matatag Grade 3.pptx
reijhongidayawan02
 
PPTX
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
PDF
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
PDF
Aprendendo Arquitetura Framework Salesforce - Dia 03
Mauricio Alexandre Silva
 
PPTX
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
Introduction to Indian Writing in English
Trushali Dodiya
 
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
TRANSLATIONAL AND ROTATIONAL MOTION.pptx
KIPAIZAGABAWA1
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
How to Create a Customer From Website in Odoo 18.pptx
Celine George
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
Difference between write and update in odoo 18
Celine George
 
Android Programming - Basics of Mobile App, App tools and Android Basics
Kavitha P.V
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
Mahidol_Change_Agent_Note_2025-06-27-29_MUSEF
Tassanee Lerksuthirat
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
PPT-Q1-WEEK-3-SCIENCE-ERevised Matatag Grade 3.pptx
reijhongidayawan02
 
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
Aprendendo Arquitetura Framework Salesforce - Dia 03
Mauricio Alexandre Silva
 
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 

Structured query language functions

  • 2. FUNCTIONS IN MYSQL There are two types of functions in MySQL: Single Row Functions : Single row functions operate on a single value to return a single value. They return only one result per row. They are further categorized into: Numeric functions String functions Date Time functions Multiple Row Functions: Multiple row functions operate on a set of rows to return a single value. Examples include SUM(), AVG() and COUNT().
  • 3. TABLE: ITEM Consider the table ITEM: Itemno Name Price Quantity DOP A001 Bread 20.25 10 2015-05-21 A002 Butter 50.75 6 2015-06-12 A003 Biscuits 10.32 20 2015-12-23 A004 Eggs 70.75 10 2015-09-18 A005 Chocolate 100.35 20 2015-10-19
  • 4. NUMERIC FUNCIONS Numeric functions perform operations on numeric values and return numeric values. There are three numeric functions which are available: POWER(X,Y) OR POW(X,Y): It returns the value of X raised to the power of Y. COMMAND OUTPUT EXPLANATION SELECT POW(2,3) ; 8 2 X 2 X 2 =8 SELECT POW(2, -2); 0.25 1/22 = ¼ = 0.25 SELECT POW(-2,2); 4 -2 X -2 = 4 SELECT POW (-2,3); -8 -2 X -2 X -2 = -8 SELECT ITEMNO, POW(PRICE,2) FROM ITEM;
  • 5. ROUND FUNCTION  ROUND(X,D) function is used to round the value of argument X upto D decimal places.  If number of decimal places is not specified or is zero, the number rounds to the nearest integer OR 0 decimal places.  If negative value is specified for precision, it counts off that value left from the decimal point. If positive value is specified for precision, it counts off that value right from the decimal point. COMMAND RESULT EXPLNATION SELECT ROUND(-4.45); -4 No. after decimal is less than 5 SELECT ROUND(-4.67); -5 No. after decimal is more than 5 SELECT ROUND(4.45); 4 No. after decimal is less than 5 SELECT ROUND(4.678,1); 4.7 No. at second place after decimal is more than 5 SELECT ROUND(4.678,0); 5 No. after decimal is more than 5 SELECT ROUND(56.345, -1); 60 SELECT ITEMNO, ROUND(PRICE) FROM ITEM;
  • 6. TRUNCATE FUNCTION  TRUNCATE(X,D) or TRUNC(X,D) returns the number X, truncated to D decimal places.  If D is 0, the result has no decimal point or fractional part.  If D is negative, it causes D digits left of the decimal point of the value X to become zero. COMMAND OUTPUT SELECT TRUNC(5.678,0); 5 SELECT TRUNC(3.768,1); 3.7 SELECT TRUNC(-9.87,1); -9.8 SELECT TRUNC(345, -2); 300 SELECT ITEMNO,TRUNC(PRICE,1) ITEM;
  • 7. STRING(CHARACTER) FUNCTION String functions operate on character type data. They return either character or numeric values. LENGTH(str): Returns the length of the string str. COMMAND OUTPUT SELECT LENGTH(“COMPUTER”); 8 SELECT LENGTH(“ I AM LEARNING”); 13 SELECT ITEMNO, LENGTH(NAME) FROM ITEM;
  • 8. CONCAT FUNCTION CONCAT(str1, str2,...) : Returns the string that results from concatenating the arguments. COMMAND OUTPUT SELECT CONCAT(“My”, “SQL”, “CLASS”); MySQLCLASS SELECT CONCAT(“My”, “SQL”,NULL, “CLASS”); NULL SELECT CONCAT(ITEMNO, NAME), PRICE FROM ITEM; CONCAT(Itemno, Name) A001Bread A002Butter A003Biscuits A004Eggs A005Chocolate
  • 9. INSTR FUNCTION INSTR(str, substr) : Returns the position of the first occurrence of substring substr in string str COMMAND RESUL T EXPLANATION SELECT INSTR(“My SQL”, “SQL); 4 First match found at 4th position SELECT INSTR(“My SQL”, “SQR”); 0 No match found SELECT INSTR(NAME, “BREAD”) FROM ITEM; INSTR(NAME,”BREAD ”) 1 0 0 0 0
  • 10. LOWER FUNCTION LOWER(str) or LCASE(str): Returns the string str in lowercase. COMMAND RESULT SELECT LOWER(“INFORMATICS”) informatics SELECT LOWER(NAME) FROM EMP; LOWER(Name) bread butter biscuits eggs chocolate
  • 11. UPPER FUNCTION UPPER(str) or UCASE(str): Returns the string str in lowercase. COMMAND RESULT SELECT UPPER(“INforMAtiCS”); INFORMATICS SELECT UPPER(NAME) FROM EMP; UPPER(Name) BREAD BUTTER BISCUITS EGGS CHOCOLATE
  • 12. LEFT FUNCTION LEFT(str, N) : Returns the N characters from the left side of the string str. COMMAND RESULT SELECT LEFT(“My SQL”, 2); My SELECT LEFT(NAME, 3) FROM ITEM; LEFT(Name, 3) Bre But Bis Egg Cho
  • 13. RIGHT FUNCTION RIGHT(str, N) : Returns the N characters from the right side of the string str. COMMAND RESULT SELECT RIGHT(“My SQL”, 3); SQL SELECT RIGHT(NAME, 3) FROM ITEM; RIGHT(Name) ead ter its ggs ate
  • 14. LTRIM FUNCTION LTRIM(str) : Returns the leading spaces ie from the left side of the string str. COMMAND RESULT SELECT LTRIM(“ INFORMATICS”); INFORMATICS SELECT LTRIM(NAME) FROM EMP; LTRIM(Name) Bread Butter Biscuits Eggs Chocolate
  • 15. RTRIM FUNCTION RTRIM(str) : Removes the trailing spaces ie from the right side of the string str. COMMAND RESULT SELECT RTRIM(“INFORMATICS ”); INFORMATICS SELECT RTRIM(NAME) FROM EMP; RTRIM(Name) Bread Butter Biscuits Eggs Chocolate
  • 16. TRIM FUNCTION TRIM(str) : Removes both leading and trailing spaces from the string str . COMMAND OUTPUT SELECT TRIM(“ INFORMATICS PRACTICES ”); INFORMATICS SELECT TRIM(NAME) FROM EMP; TRIM(Name) Bread Butter Biscuits Eggs Chocolate
  • 17. SUBSTRING/SUBSTR/MID FUNCTION SUBSTRING(str, M,N) or MID(str, M,N): Returns the specified number of characters from the middle of the string. There are 3 arguments.  The first argument is the source string.  The second argument is the position of first character to be displayed.  The third argument is the number of characters to be displayed. If the third argument is missing, then starting from the position specified, the rest of the string is returned. If the second argument is negative, then the beginning of the substring is M characters from the end of the string.
  • 18. SUBSTRING/SUBSTR/MID FUNCTION Contd… COMMAND OUTPUT SELECT SUBSTR(“WORLD OF COMPUTERS”, 4 ); or SELECT SUBSTR(“WORLD OF COMPUTERS” FROM 4 ); LD OF COMPUTERS SELECT SUBSTR(“WORLD OF COMPUTERS” , 3, 4 ); LD O SELECT SUBSTR(“WORLD OF COMPUTERS”, -4 ); TERS SELECT SUBSTR(“WORLD OF COMPUTERS”, -6,3 ); PUT SELECT SUBSTR(“WORLD OF COMPUTERS” FROM -4 FOR 2 ); TE SELECT SUBSTR(NAME,2,3) FROM ITEM; SUBSTR(Nam e,2,3) rea utt isc ggs hoc
  • 19. ASCII FUNCTION ASCII(str): Returns the ASCII value of the leftmost character of the string str.  Returns 0 if str is an empty string.  Returns NULL if str is NULL. COMMAND RESUL T EXPLANATION SELECT ASCII(‘2’); 50 ASCII value of ‘0’ is 48, ‘1’ is 49 , ‘2’ is 50 and so on SELECT ASCII(‘CG’); 67 ASCII value of ‘A’ is 65, ‘B’ is 66, ‘C’ is 67 and so on SELECT ASCII(‘are’); 97 ASCII value of ‘a’ is 97, ‘b’ is 98 and so on
  • 20. DATE AND TIME FUNCTION COMMAND EXPLANATION EXAMPLE RESULT CURDATE() Returns the current date in YYYY- MM-DD format SELECT CURDATE(); 2015-10-14 NOW() Returns the current date and time in 'YYYY-MM-DD HH:MM:SS' SELECT NOW(); 2015-10-14 20:10:30 SYSDATE() Returns the current date and time in 'YYYY-MM-DD HH:MM:SS SELECT SYSDATE(); 2015-10-14 20:10:30 DATE(exp) Extracts the date part of a date or date time expression SELECT DATE(‘2015-10-14 20:10:30’); 2015-10-14 MONTH(date) Returns the numeric month from the date passed, in the range 0 to 12 SELECT MONTH(‘2015-10- 14’); SELECT MONTH(DOP) FROM ITEM; 10
  • 21. DATE AND TIME FUNCTION Contd…… COMMAND EXPLANATION EXAMPLE RESULT YEAR(date) Returns the year for date passed in the range 0 to 9999 SELECT YEAR (‘2015-10-14’); SELECT YEAR(DAP) FROM ITEM; 2015 DAYNAME(da te) returns the name of the weekday for the date passed SELECT DAYNAME(2015-10-14); SELECT DAYNAME(DAP) FROM ITEM; MONDAY DAYOFMONT H(date) Returns the day of the month in the range 0 to 31. SELECT DAYOFMONTH(2015-10-14); SELECT DAYOFMONTH(DAP) FROM ITEM; DAYOFWEEK( date) Returns the day of week in number 1 for Sunday, 2 for Monday and so SELECT DAYOFWEEK(2015-10-14); SELECT DAYOFWEEK(DAP) FROM ITEM; DAYOFYEAR( ate) Return the day of the year for the given date in numeric format in the range 1 to 366. SELECT DAYOFYEAR(2015-10-14); SELECT DAYOFYEAR(DAP) FROM ITEM;
  • 22. SYSDATE() vs NOW() SYSDATE() returns the time at which the function executes. While NOW() which returns a constant time that indicates the time at which the statement began to execute.