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

Functions in Mysql

Uploaded by

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

Functions in Mysql

Uploaded by

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

LESSON PLAN

APRIL 2020

Class: XII(A, D, E) Subject: Informatics


Practices
AIM: To improvise their skills of programming
and arouse their interest in the other related
topics.

TOPICS:
FUNCTIONS in Mysql
A function is a predefined command set that performs
some operation and returns the single value.
Types of SQL Functions :
1) Single Row Functions
2) Multiple row Functions (Aggregate Functions)
(sum, avg, max, min, count)

Single Row Function categories:


1) Character / String Functions
2) Numeric Functions
3) Date and Time Functions
Character / String Functions :
CONCAT(), LOWER(),
UPPER(), LTRIM(),
TRIM(), RTRIM()
SUBSTR()/MID(), INSTR(),
LENGTH(), ASCII(), RIGHT(),LEFT().

Numeric Functions :
POW (), ROUND(),
TRUNCATE(), MOD()

General Format of using a function:


Ex. Result=SQRT(25) Input to a function also called as
ARGUMENT/parameter
A function can have single, multiple or no arguments at all.
Date Functions:
1. SYSDATE()
2. NOW()
3. CURDATE()
4. DATE()
5. MONTH()
6. YEAR()
7. DAYNAME()
8. DAYOFMONTH()
9. DAYOFWEEK()
10. DAYOFYEAR()
Math Functions:
1.Pow(x,y )/power(x,y): Returns the value of X raised to the
power of Y.
Example:
(i)Select POW(2,4); Result:16
(ii)POW(2,-2) Result:0.25
(iii)POW(-2,3) Result: -8
(iv) SELECT id, salary, POWER(salary,2) FROM employee;
Result:
+----+----------+-----------------+
| id | salary | power(salary,2) |
+----+----------+-----------------+
| 1 | 25000.00 | 625000000 |
| 2 | 30000.00 | 900000000 |
| 3 | 32000.50 | 1024032000.25 |
| 4 | 37500.50 | 1406287500.25 |
| 5 | 42389.50 | 1796869710.25 |
+----+----------+-----------------+
2. ROUND(X) : Rounds the argument to zero decimal place,
Where as ROUND(X, d) rounds X to d decimal places.
Example :
(i) ROUND(-1.23); Result: -1
(ii) ROUND(-1.68); Result: -2
(iii) ROUND(1.58); Result: 2
(iv) ROUND(3.798, 1); Result: 3.8
(v) ROUND(1.298, 0); Result: 1
(vi) ROUND(76823.298, -1); Result: 76820
(vii) ROUND( 25.298,-1); Result: 30
(viii) ROUND(3.798, 1); Result: 3.8
(ix) ROUND(4536.78965,-3) RESULT : 5000
(X) ROUND(4536.564553,-2) : 4500
(XI) ROUND(4586.564553,-2) : 4600
(XII)ROUND(76823.298, -2); Result:76800
XII)ROUND(76823.298, 2); RESULT: 76823.30
(XIII) ROUND(3.798, 2); Result: 3.80
3. TRUNCATE() : Truncates the argument to specified number of decimal
places.
Example:
(i)TRUNCATE (7.2978977,1) Result: 7.2
(ii)TRUNCATE(29.29,-1) Result: 20

4. SIGN() : Returns sign of a given number. IF THE ARGUMENT IS positive


then answer is 1, if the argument is negative then the answer is -1 AND
if the argument is zero then the result is zero.
Example :
(i)SIGN (15) Result : 1
(ii)SIGN (-15) Result : -1
(iii)SIGN (0) Result : 0.

5. SQRT() : Returns the square root of given number.


Example :
(i)SQRT (25) Result : 5

6. MOD(x,y): Divides x by y and gives the remainder.


Example: Note: chk for division by zero !!!!!
(i) MOD(12,5) Result: 2
CHARACTER / STRING FUNCTIONS

1. LENGTH() : Returns the length of a string in bytes/no. of


characters in string.
Example: (i) LENGTH(‘#INFOR MATICS#’); Result:14
(ii) mysql> SELECT LENGTH(First_Name) FROM Employee;
Result:
+--------------------+
| LENGTH(First_Name) |
+--------------------+
|4|
|7|
|8|
|5|
|6|
+--------------------+
5 rows in set (0.00 sec)
2. 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.
Example : ASCII(“AMAN”) Result: 65
3. CONCAT(): Returns concatenated string i.e. it adds strings.
Example :
(i)Select CONCAT(‘Informatics’,’%%% ‘,‘Practices’);
Result : Informatics%%%Practices
(ii) SELECT CONCAT ('My', 'S', 'QL'); Result: 'MySQL'
(iii) SELECT CONCAT('Class', NULL, 'XI'); Result: NULL
(iv) SELECT CONCAT(First_Name,'',Last_Name) FROM Employee;
Result:
+---------------------------------+
| CONCAT(First_Name,' ',Last_Name) |
+---------------------------------+
| Amit Sharma |
| Deeksha Verma |
| Navkiran Ahluwalia |
| Mamta Sharma |
| Bhawna Ahlurkar |
+---------------------------------+
4. INSTR(): Returns the index of the first occurrence of substring.
Example :
(i) INSTR(‘Informatics’,’ mat’);
Result : 6 (since ‘m’ of ‘mat’ is at 6th place)
(ii) SELECT INSTR ('Computers', 'pet'); Result: 0
(iii) mysql> SELECT INSTR (First_Name,'Kiran') FROM Employee;
Result:
+---------------------------+
| INSTR(First_Name,'Kiran') |
+---------------------------+
|0|
|0|
|4|
|0|
|0|
+---------------------------+
Select instr(“good morning to all”,”or”)
5. LOWER()/ LCASE(): Convert the string in lowercase.
Example: LOWER(‘INFORMATICS’); Result : informatics
6. UPPER()/ UCASE(): Convert the string in uppercase.
Example: UCASE(‘informatics’); Result : INFORMATICS
7. LEFT() : Returns the given number of characters by extracting
them from the left side of the given string
Example : LEFT(‘INFORMATICS PRACTICES’, 3);
Result : INF
8. RIGHT(): Returns the given number of characters by
extracting them from the right side of the given string
Example : RIGHT(‘INFORMATICS PRACTICES’,3);
Result : CES
9. MID()/ SUBSTR()/SUBSTRING() : Returns a substring
starting from the specified position in a given string.
Example:
(i) SUBSTR(‘INFORMATICS PRACTICES’,3,4); Result : FORM
(ii) SELECT SUBSTRING('Informatics',3); Result: 'formatics'
(iii) SELECT SUBSTRING('Computers', -3); Result: 'ers'
(iv) SELECT SUBSTRING('Computers', -5, 3); Result: 'ute'
(v) SELECT MID('Informatics',3,4); Result: 'form'
(vi) SELECT MID(first_name,3,2) FROM Employee;
Result:
+---------------------+
| MID(first_name,3,2) |
+---------------------+
| it |
| ek |
| vk |
| mt |
| aw |
+---------------------+
10. LTRIM() : Removes leading spaces.

Example : LTRIM(' INFORMATICS ');


Result: 'INFORMATICS ’

11. RTRIM(): Removes trailing spaces.

Example : RTRIM(‘ INFOR MATICS ');

Result: ‘ INFOR MATICS’

12. TRIM() : Removes leading and trailing spaces.

Example: TRIM(' $$INFOR MATICS$$ ');

Result: ‘$$INFOR MATICS$$’


Date/Time Functions

1. CURDATE() : Returns the current date.

Example: select CURDATE(); Result: '2020-04-06'

2. NOW() : Returns the current date and time

Example: select NOW(); Result : '2020-04-06 13:58:11'

3. SYSDATE() : Return the date and time at which the function executes

Example: SYSDATE(); Result: '2020-04-06 13:59:23’

4. DATE() : Extracts the date part of a date or datetime expression

Example: DATE('2020-04-06 01:02:03'); Result: '2020-04-06'


5. MONTH(): Returns the month from the date passed as
argument.
Example: MONTH('2020-03-21'); Result :3

6. YEAR() : Returns the year.

Example: YEAR('2020-03-21'); Result : 2020

7. DAYNAME() : Returns the name of the weekday.

Example: DAYNAME('2010-07-21');
Result : WEDNESDAY

8. DAYOFMONTH() : Returns the day of the month (0-31)

Example: DAYOFMONTH('2010-07-21'); Result: 21


9. DAYOFWEEK() : Returns the weekday (1-7) of the date
which is given as input
Example: DAYOFWEEK('2019-07-21'); Result: 4
(Sunday is counted as 1)
10. DAYOFYEAR() : Return the day of the year(1-366) of the
date which is given as input
(i)Example: DAYOFYEAR('2019-07-21'); Result: 202
(ii)Select id,date_join,dayofyear (date_join) from employee;
Result:
+----+------------+----------------------+
| id | date_join | dayofyear(date_join) |
+----+------------+----------------------+
| 1 | 1996-07-25 | 207 |
| 2 | 1995-06-27 | 178 |
| 3 | 1990-02-20 | 51 |
| 4 | 1989-08-18 | 230 |
| 5 | 2010-03-01 | 60 |
+----+------------+----------------------
Try the following output
questions:
•Select ROUND (1023.431,1);
•Select LENGTH(‘RAM’);
• Select MOD(ROUND(120.60),5);
•Select trim(‘ mount carmel school ‘);
•Select Month(sysdate());
•Select length(SUBSTR(Trim(‘ India is great ‘),3,9));
•select MONTH(‘2009-09-19’);
•select YEAR(CURDATE())-YEAR(‘2008-01-01’);
Select 10-5;
ASSIGNMENT
Q1 Consider an emp table ( empno, ename, job, sal,
hiredate, deptno) and write SQl queries to do the
following:
a) Display the names IN LOWERCASE of the
employees along with their department names.
b) Display the joining date of every emp along with
the date when he will complete his 20 years of
service.
c) Display the name concatenated with the jobs for all
the employees.

You might also like