0% found this document useful (0 votes)
11 views8 pages

Dbmsunit2 Setop (It)

Uploaded by

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

Dbmsunit2 Setop (It)

Uploaded by

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

Built in Functions in MySQL | Numeric, String

and Date Time Functions in MySQL




MySQL functions are powerful tools that help in performing complex


calculations and data manipulation with ease. The functions in MySQL can
edit rows and tables, alter strings, and help you manage organized and
easy-to-navigate databases.
A function is a special type of predefined command set that performs some
operation and returns a value. Functions operate on zero, one, two, or more
values that are provided to them. The values that are provided to functions are
called parameters or arguments.
The MySQL functions have been categorized into various categories, such
as String functions, Mathematical functions, Date and Time
functions, etc.
In this article, we will look at these different categories of MySQL functions,
and look at different MySQL functions with definitions and examples in each
category. Three basic and important MySQL function categories are:

● String Functions
● Numeric Functions
● Date and Time Functions

MySQL String Functions


The string functions of MySQL are used to manipulate and transform the
text string. Some commonly used string functions are:
S.No. Function Description Examples

Returns the character 1. SELECT CHAR(70, 65, 67, 69) ;


1. definitions
for each integer passes 2. SELECT CHAR(65, 67.3, 69.3) ;

SELECT CONCAT(name, aggregate)


Returns concatenated AS “Name Marks”
2. CONCAT()
string FROM student WHERE age = 14 OR
age = 16;
S.No. Function Description Examples

SELECT
LOWER(‘GEEKSFORGEEKS’) AS
LOWER() Returns the argument in
3. “LowerName1”,
/LCASE() lowercase
LOWER(‘Geeks For Geeks’) AS
“LowerName2” ;

1. SELECT SUBSTR(‘ABSDEFG’,
SUBSTRING() Returns the substring as 3, 4) “Subs” ;
4.
, SUBSTR() specified 2. SELECT SUBSTR(‘ABCDEFG’,
-5, 4) “Subs” ;

SELECT UPPER(‘Large’)
UPPER()/UCA “Uppercase” ;
5. Converts to uppercase
SE() or SELECT UCASE(‘Large’)
“Uppercase”;

Removes leading and


6. TRIM() SELECT TRIM(‘Bar One’) ;
trailing spaces

Returns the length of a SELECT LENGTH(‘CANDIDE’)


7. LENGTH()
string in bytes “Length in characters” ;

MySQL Numeric Functions


The numeric functions in MySQL accept numeric values, perform a
mathematic operation on the values and return resulting sheet. Some useful
numeric functions are:

S.No. Function Description Example

Returns the remainder of one


SELECT MOD(11, 4)
1. MOD() expression by diving y another
“Modulus” ;
expression.
S.No. Function Description Example

Returns the value of one


POWER()/POW( SELECT POWER(3, 2)
2. expression raised to the power of
) “Raised” ;
another expression

Returns numeric expression


SELECT
rounded to an integer. Can be used
3. ROUND() ROUND(15.193, 1)
to round an expression to a
“Round” ;
number of decimal points.

This function returns sign of a SELECT SIGN(-15)


4. SIGN()
given number. “Sign” ;

Returns the non-negative square SELECT SQRT(26)


5. SQRT()
root of numeric expression. “Square root” ;

Returns numeric exp1 truncate to


DRLRCT
exp2 decimal places. If exp2 is 0,
6. TRUNCATE() TRUNCATE(15.79, 1)
then the result will have no
“Truncate” ;
decimal point

MySQL Date and Time Functions


Date and Time functions in SQL are used to manipulate and transform date
and time data stored in tables. Date functions operate on values of the DATE
datatype.
Some useful date and time functions are:

S.No. Function Description Example

CURDATE()/
1 CURRENT_DATE()/ Returns the current date. SELECT CURDATE() ;
CURRENT_DATE

Extracts the date part of


SELECT DATE(‘2020-12-31
2 DATE() a date or date-time
01:02:03’) ;
expression.
S.No. Function Description Example

Returns the month from SELECT


3 MONTH()
the date passed. MONTH(‘2020-12-31’) ;

4 YEAR() Returns the year SELECT YEAR(‘2020-12-31’) ;

Returns the time at


5 NOW() which the function SELECT NOW() ;
executes.

SELECT NOW(), SLEEP(2),


Returns the current date NOW() ;
6 SYSDATE()
and time. or SELECT SYSDATE(),
SLEEP(2), SYSDATE() ;

SQL Set Operation


The SQL Set operation is used to combine the two or more SQL SELECT statements.

Types of Set Operation


1. Union
2. UnionAll
3. Intersect
4. Minus
1. Union
ADVERTISEMENT
ADVERTISEMENT

o The SQL Union operation is used to combine the result of two or more SQL SELECT
queries.
o In the union operation, all the number of datatype and columns must be same in
both the tables on which UNION operation is being applied.
o The union operation eliminates the duplicate rows from its resultset.

Syntax
1. SELECT column_name FROM table1
2. UNION
3. SELECT column_name FROM table2;

Example:

The First table

ADVERTISEMENT

ID NAME

1 Jack

2 Harry

3 Jackson

The Second table

ID NAME

3 Jackson

4 Stephan

5 David

Union SQL query will be:

1. SELECT * FROM First


2. UNION
3. SELECT * FROM Second;

The resultset table will look like:

ID NAME

1 Jack

2 Harry

3 Jackson
4 Stephan

5 David

2. Union All
Union All operation is equal to the Union operation. It returns the set without removing
duplication and sorting the data.

Syntax:

1. SELECT column_name FROM table1


2. UNION ALL
3. SELECT column_name FROM table2;

Example: Using the above First and Second table.

Union All query will be like:

1. SELECT * FROM First


2. UNION ALL
3. SELECT * FROM Second;

The resultset table will look like:

ID NAME

1 Jack

2 Harry

3 Jackson

3 Jackson

4 Stephan

5 David

3. Intersect
o It is used to combine two SELECT statements. The Intersect operation returns the
common rows from both the SELECT statements.
o In the Intersect operation, the number of datatype and columns must be the same.
o It has no duplicates and it arranges the data in ascending order by default.

Syntax

1. SELECT column_name FROM table1


2. INTERSECT
3. SELECT column_name FROM table2;

Example:

Using the above First and Second table.

Intersect query will be:

1. SELECT * FROM First


2. INTERSECT
3. SELECT * FROM Second;

You might also like