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

SQL Functions Notes

The document provides an overview of various MySQL functions organized into categories such as text, mathematical, and date/time functions. It describes functions like CHAR(), CONCAT(), LOWER(), UPPER(), SUBSTR(), TRIM(), LTRIM(), RTRIM(), INSTR(), LENGTH(), LEFT(), RIGHT(), MOD(), POWER(), ROUND(), SIGN(), SQRT(), TRUNCATE(), CURDATE(), DATE(), MONTH(), MONTHNAME(), DAY(), YEAR(), DAYNAME(), DAYOFMONTH(), DAYOFWEEK(), DAYOFYEAR(), DATEDIFF(), NOW(), and SYSDATE() and provides examples of how each function is used.

Uploaded by

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

SQL Functions Notes

The document provides an overview of various MySQL functions organized into categories such as text, mathematical, and date/time functions. It describes functions like CHAR(), CONCAT(), LOWER(), UPPER(), SUBSTR(), TRIM(), LTRIM(), RTRIM(), INSTR(), LENGTH(), LEFT(), RIGHT(), MOD(), POWER(), ROUND(), SIGN(), SQRT(), TRUNCATE(), CURDATE(), DATE(), MONTH(), MONTHNAME(), DAY(), YEAR(), DAYNAME(), DAYOFMONTH(), DAYOFWEEK(), DAYOFYEAR(), DATEDIFF(), NOW(), and SYSDATE() and provides examples of how each function is used.

Uploaded by

Tall Shadows
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Class 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.

MySQL functions have been categorized into various categories such as


 Text functions (String) (A-65 to Z-90) , (a-97 to z-122)
 Mathematical Functions (Numeric)
 Date and Time functions.

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.

mySql>select CHAR (70,65,67,69);

MySql>Select Char(65, 67.3,69.3);

mySql>select CHAR (111, 112,106,115);

2. Concat(str1,str2,…): It returns argument str1 concatenated with argument str2


mysql> SELECT CONCAT(FIRST_NAME,SALARY AS 'NAME SALARY' FROM TEACHER;

3. LOWER()/LCASE(): It returns argument str, with all letters in lowercase.


mysql> SELECT LOWER(FIRST_NAME) FROM TEACHER;

4. UPPER()/UCASE(): It returns argument str, with all letters uppercase.


mysql> SELECT UCASE('pqrxyz');

5. SUBSTR() : It returns a portion of str, beginning at character m, n characters long.


SUBSTR(str, m[‘n])
SUBSTR(str from m [For n])
If m is negative the counting starts from back.
If n is omitted , MySQL returns all the characters.
If n is less than 1, a null is returned.
mysql> select substr('ABCDEFGH', 3,4);
mysql> select substr('ABCDEFGH', -3,4);
6. TRIM()
TRIM([{BOTH | LEADING | TRAILING} [REMSTR] FROM] STR), TRM([REMSTR from] STR)
• It returns the str with all remstr prefixes or suffixes removed.
• If none of the specifiers BOTH, LEADING or TRAILING is given, BOTH is assumed.
• Remstr is optional and , if not specified , spaces are removed.
mysql> select trim(leading 'X' from 'XXXpqrXXX Periodic Test 1 is over XXXpqrXXX ');
mysql> select trim(trailing 'X' from 'XXXpqrXXX Periodic Test 1 is over XXXpqrXXX');

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

1. MOD(): This function return modulus of given two numbers.


Syntax: MOD(m,n), m%n, m MOD n
mysql> select MOD(23,5);

2. POWER/POW(): This function returns mn , a number m raised to the nth power.


Syntax: POWER(m,n) or POW(m,n)
mysql> select power(3,4);

3. ROUND(): This function returns a number rounded off as per given specifications.
Syntax: ROUND(n[,m])
mysql> select ROUND(20.392,1);

4. SIGN() : This function returns sign of a given number.


If argument n<0, the function returns -1. If argument n=0, the function returns 0.
If argument n>0, the function returns 1.
mysql> select sign(-945);
mysql> select sign(945);

5. SQRT(): This function returns the square root of the given number.
Syntax: SQRT(n)
mysql> select sqrt(25);

6. TRUNCATE() : Returns numeric exp1 truncated to exp2 decimal places.


Syntax: TRUNCATE(n,m)
mysql> select truncate(129.345,1);
mysql> select truncate(129.345,2);

DATE & TIME FUNCTIONS

1. CURDATE() / CURRENT_DATE(): This function returns the current date.


mysql>SELECT CURDATE();
mysql>SELECT CURDATE() + 10;

2. DATE(): This function extracts the date part of a datetime expression


Syntax: DATE(expr)
mysql>SELECT DATE(‘2021-03-31 01:02:03’);

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());

5. DAY(): This function returns the day part of a date.


Syntax: DAY(date)
mysql>SELECT DAY(‘2020-09-10’);
mysql>SELECT DAY(CURDATE());

6. YEAR(): This function returns the year part of a date.


Syntax: YEAR(date)
mysql>SELECT YEAR(‘2021-09-10’);
mysql>SELECT YEAR(CURDATE());

7. DAYNAME(): This function returns the name of weekday.


Syntax: DAYNAME(date)
mysql>SELECT DAYNAME(‘2020-09-10’);
mysql>SELECT DAYNAME(CURDATE());
8. DAYOFMONTH(): This function returns the day of month.
Syntax: DAYOFMONTH(date)
mysql>SELECT DAYOFMONTH(‘2020-08-31’);
mysql>SELECT DAYOFMONTH(CURDATE());

9. DAYOFWEEK(): This function returns the day of week.


(1=Sunday, 2=Monday, …..)
Syntax: DAYOFWEEK(date)
mysql>SELECT DAYOFWEEK(‘2021-03-31’);
mysql>SELECT DAYOFWEEK(CURDATE());

10. DAYOFYEAR(): This function returns the day of the year.


Syntax: DAYOFYEAR(date)
mysql>SELECT DAYOFYEAR(‘2021-02-25’);
mysql>SELECT DAYOFYEAR(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();

Note: ‘Content developed/prepared absolutely from home.

You might also like