SQL Functions Notes
SQL Functions Notes
Class: XII
Topic: Unit-1:
Subject: Information Technology (802) Database Concepts (MySQL Functions)
MySQL Functions
A function is a special type of predefined command set that performs some operations and returns a single
value. The values that are provided to functions are called parameters or arguments.
1. CHAR(): It returns a string made up of the ASCII representation of the decimal value list. Strings in
numeric format are converted to a decimal value. Null values are ignored.
7. LTRIM(): It removes the spaces from the left of the given string.
mysql> SELECT LTRIM(' abcdefgh');
8. RTRIM() It removes the spaces from the right of the given string.
mysql> SELECT RTRIM('abcdefgh ');
9. INSTR: This function searches for given second string into the given first string.
mysql> select instr('O P JINDAL SCHOOL, KHARSIA ROAD RAIGARH, JINDAL STEEL AND
POWER LIMITED','JINDAL');
10. LENGTH:
mysql> SELECT LENGTH('O P JINDAL SCHOOL, RAIGARH');
mysql> SELECT LENGTH(12345 );
11. LEFT: Returns the leftmost len characters from the string str.
Returns NULL if any argument is NULL.
mysql> select left('JS12345/OPJS/XII/G',7);
mysql> select substr('JS12345/OPJS/XII/G',9,4);
12. RIGHT:
Returns the rightmost len characters from the string str.
mysql> select right('JS12345/OPJS/XII/A',5);
NUMERIC FUNCTIONS
3. ROUND(): This function returns a number rounded off as per given specifications.
Syntax: ROUND(n[,m])
mysql> select ROUND(20.392,1);
5. SQRT(): This function returns the square root of the given number.
Syntax: SQRT(n)
mysql> select sqrt(25);
3. MONTH(): This function returns the month from the date passed.
Syntax: MONTH(date)
mysql>SELECT MONTH(‘2020-09-10’);
mysql>SELECT MONTH(CURDATE());
4. MONTHNAME(): This function returns the name of the month for a date.
Syntax: MONTHNAME(date)
mysql>SELECT MONTHNAME(‘2021-04-10’’);
mysql>SELECT MONTHNAME(CURDATE());
11. DATEDIFF(): Returns the number of days between two date values.
Syntax: DATEDIFF(date1, date2)
mysql>SELECT DATEDIFF('2017-06-25','2017-06-15');
mysql>SELECT DATEDIFF('2017-06-25 09:34:21','2017-06-15 15:25:35');
12. NOW(): This function returns the current date and time.
Syntax: NOW()
mysql>SELECT NOW();
13. SYSDATE(): This function returns the current date and time. But, it returns the time at which the
function executes.
Syntax: SYSDATE()
mysql>SELECT SYSDATE();