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

Functions in Mysql

Uploaded by

sadafanjum042004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Functions in Mysql

Uploaded by

sadafanjum042004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

FUNCTIONS

IN
MYSQL
MYSQL FUNCTIONS
There are three types of MySQL functions:

MATHEMATICAL FUNCTIONS

 STRING FUNCTIONS

 DATE FUNCTIONS
MATHEMATICAL FUNCTIONS
MATHEMATICAL FUNCTIONS
 POWER() OR POW()
This function return mn i.e. number m raised to the nth power.
Syntax:
POWER(m,n) or POW(m,n)
Examples:
SELECT POWER(2,3); #Output - 8
SELECT POWER(2,-2); #Output - 0.25
SELECT POW(4,2); #Output - 16
 ROUND()
This function return value of argument n rounded to m places right to the decimal
point. If m is omitted than it rounds the number to 0 th place.
Syntax:
ROUND(n,m) or ROUND(n)
Examples:
SELECT ROUND(1.58); # Output will be 2
SELECT ROUND(1.298,1); # Output will be 1.3
SELECT ROUND(-1.23); # Output will be -1
SELECT ROUND(-1.58); # Output will be -2
SELECT ROUND(4.368,2); # Output will be 4.37
MATHEMATICAL FUNCTIONS
 MOD(m,n), M%N, M MOD N
• This function returns remainder of argument m divided by argument n.
• If n, is 0 then it will return NULL.

Syntax:
MOD(M,N)
Examples:
SELECT MOD(10,3); # Output will be 1
SELECT MOD(10,4); # Output will be 2
SELECT 15 MOD 3; # Output will be ___________
SELECT 30%8; # Output will be ___________
MATHEMATICAL FUNCTIONS
 TRUNCATE()
• This function truncates the specified number to the specified decimal places.
• If no decimal place is specified then it return the number without any decimal.
• Truncate function never rounds off the value. It simply deletes the number
after the decimal point.

Syntax:
TRUNCATE(n,d)
Examples:
SELECT TRUNCATE(5.253,1); # Output will be 5.2
SELECT TRUNCATE(3.99,0); # Output will be 3
SELECT TRUNCATE(3.99); # Output will be 3
SELECT TRUNCATE(-1.99,1); # Output will be -1.9
STRING FUNCTIONS
STRING FUNCTIONS
 CHAR()
• This function return the character for each integer passed as arguments.
• It return the character on the bases of the ASCII value of the character.
• If any argument contains a fractional value or string value then these values
are considered as integer by default.
• ASCII values : A-Z = 65-90 , a-z = 97-122
Syntax:
CHAR(value1, value2,……)
Examples:
SELECT CHAR(65, 67, 69); #Output - ACE
SELECT CHAR(65.9, 67, ‘69’); #Output - ACE
 CONCAT()
This function is used to concatenate the two strings.
Syntax:
CONCAT(str1,str2)
Examples:
SELECT CONCAT(“DPS”,”RANIPUR”); # Output will be DPSRANIPUR
STRING FUNCTIONS Continue……

 LOWER() or LCASE()
• This function converts the given string into lower case(small letters).
Syntax:
LOWER(str) or LCASE(str)
Examples:
SELECT LOWER(“DPS”); # Output - dps
SELECT LCASE(“Dps Ranipur”); #Output - dps ranipur

 UPPER() or UCASE()
• This function converts the given string into upper case(capital letters).
Syntax:
UPPER(str) or UCASE(str)
Examples:
SELECT UPPER(“Dps”); # Output - DPS
SELECT UCASE(“Dps Ranipur”); #Output – DPS RANIPUR
STRING FUNCTIONS Continue……
 LENGTH()
• This function returns the length of the given string including letters, digits,
space and other symbols.
Syntax: LENGTH(str)
Examples:
SELECT LENGTH(“DPS”); # Output -3
SELECT LENGTH(“Dps Ranipur”); #Output - 11
 SUBSTR()
• This function extract the substring starting from the specified position in a
given string.
Syntax: SUBSTR(str, m, n)
• Here m represent the starting position of substring and n represents the
number of characters in the substring. if m is positive then it counts from the
beginning and if m is negative it counts from the end.
Examples:
SELECT SUBSTR(“DPSRANIPUR”,4,3); # Output - RAN
SELECT SUBSTR(“DPSRANIPUR”,4); # Output – RANIPUR
SELECT SUBSTR(“DPSRANIPUR”,-5,3); # Output – NIP
STRING FUNCTIONS Continue……
 LEFT()
• This function returns the leftmost n characters from the given string.
Syntax: LEFT(str, n)
Examples:
SELECT LEFT(“DPSRANIPUR”,4); # Output - DPSR

 RIGHT()
• This function returns the rightmost n characters from the given string.
Syntax: RIGHT(str, n)
Examples:
SELECT RIGHT(“DPSRANIPUR”,3); # Output – PUR
 INSTR()
• This function returns the position of the first occurrence of substring in the
given string.
Syntax: INSTR(str, substr)
Examples:
SELECT INSTR(“DPSRANIPUR” , ’R’); # Output – 4
SELECT INSTR(“DPSRANIPUR” , ’PUR’); # Output – 8
STRING FUNCTIONS Continue……
 TRIM()
• This function removes the leading and trailing spaces from a given string.
• By default it removes the spaces from both sides.
Syntax: TRIM(str) or TRIM(BOTH/LEADING/TRAILING remstr FROM str)

Examples:
(i) To remove the spaces from both side of given string
SELECT TRIM(“ DPS ”); # Output – DPS (without
space)

(ii) To remove the leading @ from the given string


SELECT TRIM(“LEADING ‘@’ FROM ‘@@@DPS@@’); # Output – DPS@@

(iii) To remove the trailing @ from the given string


SELECT TRIM(“TRAILING ‘@’ FROM ‘@@@DPS@@’); # Output – @@@DPS

(iv) To remove @ from both sides of the given string


SELECT TRIM(“BOTH ‘@’ FROM ‘@@@DPS@@’); # Output – DPS
STRING FUNCTIONS Continue……
 LTRIM()
• This function removes the leading spaces from a given string.
Syntax:
LTRIM(str)
Examples:
To remove the leading spaces from the given string
SELECT LTRIM(“ DPS”); # Output – DPS (without
space)

 RTRIM()
• This function removes the trailing spaces from a given string.
Syntax:
RTRIM(str)
Examples:
To remove the trailing spaces from the given string
SELECT RTRIM(“ DPS ”); # Output – DPS (with space)
STRING FUNCTIONS Continue……

 MID()
• This function extract the substring starting from the specified position in a
given string.
Syntax: MID(str, m, n)
• It is similar to SUBSTR() function
Examples:
SELECT MID(“DPSRANIPUR”,4,3); # Output - RAN
SELECT MID(“DPSRANIPUR”,4); # Output – RANIPUR
DATE FUNCTIONS
DATE FUNCTIONS
 CURDATE() or CURRENT_DATE()
• This function returns the current date in the format YYYY-MM-DD.
Syntax:
CURDATE() or CURRENT_DATE()
Examples:
SELECT CURDATE(); # Output – 2020-05-15

 DATE()
• This function extract the date part from the datetime expression.
Syntax: DATE(exp)
Examples:
SELECT DATE(“2020-05-15 09:15:20”); # Output – 2020-05-15
 MONTH()
• This function returns the month from the date passed.
Syntax: MONTH(date)
Examples:
SELECT MONTH(“2020-05-15”); # Output – 5
DATE FUNCTIONS
 YEAR()
• This function returns the year from the date passed.
Syntax:
YEAR(date)
Examples:
SELECT YEAR(“2020-05-15”); # Output – 2020
 DAYNAME()
• This function return the name of the weekday.
Syntax:
DAYNAME(date)
Examples:
SELECT DAYNAME(“2020-05-15”); # Output – Friday
 DAYOFMONTH() or DAY()
• This function returns the day of month.
Syntax: DAYOFMONTH(date)
Examples:
SELECT DAYOFMONTH(“2020-05-15”); # Output – 15
DATE FUNCTIONS
 DAYOFYEAR()
• This function returns the day of the year.
Syntax:
DAYOFYEAR(date)
Examples:
SELECT DAYOFYEAR(“2020-05-15”); # Output – 136

 DAYOFWEEK()
• This function returns the day of week.
Syntax: DAYOFWEEK(date)
Examples:
SELECT DAYOFWEEK(“2020-05-15”); # Output – 6

 NOW()
• This function returns the current date and time .
Syntax: NOW()
Examples:
SELECT NOW(); # Output – 2020-05-15 09:36:22
Quiz
Write your answer in the chat box

1 What will be the output of the following query?


SELECT ROUND(5.2546,2);
a. 5.25
b. 5.26
c. 5.2
d. 5.20
2 What will be the output of the following query?
SELECT SUBSTR(“ONLINECLASS”,3,5);
a. INECL
b. LINEC
c. INE
d. LIN
3 What will be the output of the following query?
SELECT TRUNCATE(4.3589,2);
a. 4.35
b. 4.36
c. 4.00
d. 4.30
4 What will be the output of the following query?
SELECT INSTR(“DATABASE MANAGEMENT”,”AGE”);
a. 12
b. 13
c. 14
d. 11
Thank You

You might also like