×

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 Descending Index

Descending Index It is a type of index that stores key values in descending order. This query improves the performance of an index query. MySQL system displays the index as per...

3 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 View

Introduction MySQL View is a virtual table to create a clone of the base table. The View does not contain its values or data. MySQL View creates to connect more than...

12 minutes read.

MySQL Union

MySQL contains the same categories of data in a different table. Sometimes, you require multiple tables to collect data together. MySQL union is a function to combine two table's data...

5 minutes read.

MySQL LOG() function

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

3 minutes read.

MySQL FLOOR() function

In this context, we will learn how we can use the MySQL FLOOR() function with proper syntaxes and examples. Introduction of MySQL Floor() function: FLOOR() function in MySQL is used to return...

3 minutes read.

MySQL NOW() function

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

2 minutes read.

MySQL COUNT function

The count function returns the number of rows in the table. This function shows either the entire rows count or the required row count of the table. It determines the...

5 minutes read.

MySQL GREATEST function

This statement displays the greatest values of the table. The GREATEST function returns a null value when the table contains a null value. The GREATEST function needs a minimum of...

2 minutes read.

MySQL QUARTER() function

In this context, we will learn how we can use the MySQL QUARTER() function with proper syntax and good examples. Introduction of MySQL QUARTER() function The QUARTER() function in MySQL returns the...

3 minutes read.

MySQL CONCAT() function

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

3 minutes read.

MySQL LOAD_FILE() function

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

3 minutes read.

MySQL CEILING() function

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

2 minutes read.

MySQL Environmental Setup

The MySQL Environmental Setup MySQL is free, open-source, and cross-platform software, which can be downloaded from its official website. MySQL management system installs on Linux, macOS, and windows. MySQL requires a framework...

6 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 LOG10() function

In this context, we will learn how we can use the MySQL LOG10() function with proper syntax and good examples. Introduction of MySQL LOG10() function To evaluate the natural logarithmic value of...

2 minutes read.

MySQL String Function

The string function is used to maintain and operate string values. This function modifies the string data as per function. We can do the concatenation, conversion, removal and replacing the...

4 minutes read.

MySQL Create index

An index creation helps to make a row of the table unique. The index operates and handles table data quickly. MySQL index requires NOT NULL column constraint. The index column...

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