How the ADDDATE() function works in Mariadb?

The ADDDATE() function in MariaDB that allows you to add a specified time interval to a date or datetime value, resulting in a new date or datetime value.

Posted on

The ADDDATE() is a function in MariaDB that allows you to add a specified time interval to a date or datetime value, resulting in a new date or datetime value.

Syntax

The syntax for the MariaDB ADDDATE() function is as follows:

ADDDATE(date, INTERVAL expr unit)

date is the starting date or datetime value. expr is the expression specifying the amount of time to add, and unit is the unit of time to be added, such as DAY, MONTH, YEAR, etc.

Examples

Adding Days to a Date

To add a number of days to a specific date:

SELECT ADDDATE('2023-03-16', INTERVAL 10 DAY);
2023-03-26

This adds 10 days to March 16, 2023, resulting in March 26, 2023.

Adding Months

To add months to a date:

SELECT ADDDATE('2023-03-16', INTERVAL 2 MONTH);
2023-05-16

Adding 2 months to March 16, 2023, gives us May 16, 2023.

Adding Years

To add years to a date:

SELECT ADDDATE('2023-03-16', INTERVAL 1 YEAR);
2024-03-16

This adds 1 year to March 16, 2023, resulting in March 16, 2024.

Subtracting Time

To subtract time, use a negative interval:

SELECT ADDDATE('2023-03-16', INTERVAL -1 MONTH);
2023-02-16

Subtracting 1 month from March 16, 2023, gives us February 16, 2023.

Adding Time to the Current Date

To add time to the current date:

SELECT ADDDATE(CURRENT_DATE(), INTERVAL 1 WEEK);
2024-03-23

This adds 1 week to the current date.

Here are a few functions related to MariaDB’s ADDDATE():

  • MariaDB SUBDATE() function is used to subtract a specified time interval from a date.
  • MariaDB DATE_ADD() function is an alias for ADDDATE().
  • MariaDB DATE_SUB() function subtracts a specified time interval from a date.

Conclusion

The ADDDATE() function is a versatile tool in MariaDB for date arithmetic. It’s useful for calculating deadlines, scheduling events, and manipulating date and datetime values in various applications. By understanding how to use ADDDATE() effectively, you can perform a wide range of date-related operations with ease and precision.