×

MySQL PERIOD_ADD() function

In this context, we will learn how we can use the MySQL PERIOD_ADD() function with proper syntax and good examples.

Introduction of MySQL PERIOD_ADD() function

In MySQL, PERIOD_ADD() function will help to add a specific number of months to a given period. The PERIOD_ADD() function will return the resultant value in the ‘YYYYMM‘ format.

Syntax of the MySQL PERIOD_ADD() function

The syntax of the MySQL PERIOD_ADD() function is given as follows:

PERIOD_ADD(prd, num)

Parameters or arguments used in MySQL PERIOD_ADD() function:

There are two parameters accepted by the PERIOD_ADD() function in MySQL, which are given as follows:

PRD:

This is the period that should be in YYMM OR YYYYMM format.

Num:

This parameter is the number of months that will be added to a given period; the value can be negative or positive.

Returns:

The function will return the resultant value after adding a specific number of months to the given period.

Application used for PERIOD_ADD() function:

The PERIOD_ADD() function can be used in the given below MySQL versions.:

  • MySQL 5.7
  • MySQL 5.6
  • MySQL 5.5
  • MySQL 5.1
  • MySQL 5.0
  • MySQL 4.1
  • MySQL 4.0
  • MySQL 3.23

Examples of MySQL PERIOD_ADD() function:

Now we will look into some MySQL PERIOD_ADD() function examples and will explore how we can use the PERIOD_ADD function in MySQL.

Example 1 :

In this example, we will add months to a given period using PERIOD_ADD() function in MySQL.

SELECT PERIOD_ADD(202011, 9) As New_period;

Output:

New_period
202108
1 row in set (0.00 sec)

Example 2:

In this example, we will subtract months from a given period using PERIOD_ADD() function in MySQL.

SELECT PERIOD_ADD(202102, -5) As New_period;

Output:

New_period
202009
1 row in set (0.00 sec)

Example 3:

In the given example, we will add and Subtract months from a two-digit year period in MySQL.

SELECT
PERIOD_ADD(2109, -5) As New_period1,
PERIOD_ADD(2109, +5) As New_period2;

Output:

New_period1New_period2
202104202202
1 row in set (0.00 sec)

Example 4:

In this example, we will use the Current Date and Extract functions in MySQL.

SELECT
CURDATE( ) AS 'Curr_date',
EXTRACT(YEAR_MONTH FROM CURDATE( )) AS 'Curr_period',
PERIOD_ADD(EXTRACT(YEAR_MONTH FROM CURDATE( )), 11) AS 'New_period';

Output:

Curr_dateCurr_periodNew_period
2020-11-30202011202110

Application of MySQL PERIOD_ADD() function:

This function is used to find the Remainder of one number divided by another.

Summary:

In the above context, we have learned how we can use the PERIOD_ADD() function in MySQL used to find the Remainder of one number divided by another.


Related Topics

MySQL PI() function

In this context, we will learn how we can use the MySQL PI() function with proper syntax and good examples. Introduction of MySQL PI() function PI() function in MySQL is used to...

2 minutes read.

MySQL RTRIM() function

In this context, we will learn how we can use the MySQL RTRIM() function with proper syntax and good examples. Introduction of MySQL RTRIM() function It is the function in MySQL that...

2 minutes read.

MySQL Aggregate function

The aggregation function works on the multiple values and returns single value output.  It makes the advanced and complex operation simple. The summation (SUM), MAX, AVG, MIN, COUNT functions are...

5 minutes read.

MySQL: Entity-Relationship Model

Entity-Relationship Model Entity-Relationship model or E R model is used to create a relationship between different attributes or entities. It describes the structure of the database with the help of the...

5 minutes read.

MySQL Vs MongoDB

What is MongoDB? MongoDB is a distributed, open-source, cross-platform document-based database which was created to scale and develop applications simply. It was created as a NoSQL database by MongoDB Inc. MongoDB gets...

6 minutes read.

MySQL Full text index

Full text index The full-text index in the table isused to search the full text of the data. This index assigns to the table using a "FULLTEXT" keyword. First, the table...

3 minutes read.

MySQL Subquery

The subquery is a MySQL query used to manage data operations. Mainly subquery helps to retrieve data with the necessary condition. It creates a nested query with two different queries....

5 minutes read.

MySQL Clauses

MySQL Clauses MySQL system clauses are keywords or statements to handle information. It helps to operate a group of the data and apply it to require conditions. The clauses apply conditions...

16 minutes read.

MySQL Table

Introduction MySQL table is an essential part of the system. MySQL table stores data using index, rows, and columns. It accesses data from the server quickly because of the table—this table...

22 minutes read.

find_in_set() function in MySQL

This Function is used for finding the position of a particular string from the list of strings. For suppose if the specified string is repeated multiple times, then this functions...

4 minutes read.

MySQL EXPORT_SET() function

In this context, we will learn how we can use the MySQL EXPORT_SET() function with proper syntax and good examples. Introduction of MySQL EXPORT_SET() function This function returns a string and shows...

3 minutes read.

MySQL LOG2() function

In this context, we will learn how we can use the MySQL LOG2() function to calculate the Logarithm of a specific number with base 2 with proper syntax and examples. Introduction...

2 minutes read.

MySQL Index

An index is a widely used method to access table information quickly. MySQL requires an index for operating table data stored in rows and columns. It's an entry point of...

3 minutes read.

MySQL Advance function

The advance function operates numerical values, string values, and data types.  The advance function converts, displays, and compares given values as per requirement. Here, you find out database, table, and...

4 minutes read.

MySQL LIKE() function

In this context, we will learn how we can use the MySQL LIKE() function with proper syntax and good examples. Introduction of MySQL LIKE() function The LIKE function is a logical function...

4 minutes read.

MySQL Data Types

Introduction of the Data types In the data file, we have several information such as names, numbers, marks, and other information. This information specifies categories such as numbers, characters, and decimal...

8 minutes read.

MySQL Operators

MySQL Operators MySQL operator needs advanced operation on the table and its data. This operator works with the "WHERE" clause. MySQL operators are a statement to modify information. It helps to...

13 minutes read.

MySQL PERIOD_ADD() function

In this context, we will learn how we can use the MySQL PERIOD_ADD() function with proper syntax and good examples. Introduction of MySQL PERIOD_ADD() function In MySQL, PERIOD_ADD() function will help to...

2 minutes read.

MySQL Truncate() Function

In this context, we will learn how we can use the MYSQL TRUNCATE function to truncate a number to a mentioned number of decimal places. Syntax of MySQL Truncate Function A number...

2 minutes read.

MySQL INNER JOIN

MySQL inner join connects two tables by using their common columns. It is the basic join of the MySQL system. It is used to returns only those results from the...

4 minutes read.