MySQL TIMESTAMPADD() Function

Timestampadd() function in MySQL; In this tutorial, i am going to show you how to add the specified time of given date or DateTime value using MySQL TIMESTAMPADD() function.

MySQL TIMESTAMPADD() Function

MySQL TIMESTAMPADD() adds time value with a date or datetime value.

Syntax of MySQL TIMESTAMPADD() Function

The basic syntax of this function is:

TIMESTAMPADD(unit,interval,datetime_expr)

Here, unit is the unit to add, interval is how many of the units to add, and datetime_expr is the initial date or datetime value.

The unit argument of timestampadd() function; as following:

  • MICROSECOND
  •  SECOND
  •  MINUTE
  • HOUR
  • DAY
  • WEEK
  • MONTH
  • QUARTER
  • YEAR

Example 1 – MySQL TIMESTAMPADD() Function

To add a day to the initial date using the timestampadd() function; as follows:

SELECT TIMESTAMPADD(DAY, 1, '1999-12-31');

Output-1

+------------------------------------+
| TIMESTAMPADD(DAY, 1, '1999-12-31') |
+------------------------------------+
| 2000-01-01                         |
+------------------------------------+

Example 2 – MySQL TIMESTAMPADD() Function

To add a second to the initial date using the mysql timestampadd() function; as follows:

SELECT TIMESTAMPADD(SECOND, 1, '1999-12-31');

Output-2

+---------------------------------------+
| TIMESTAMPADD(SECOND, 1, '1999-12-31') |
+---------------------------------------+
| 1999-12-31 00:00:01                   |
+---------------------------------------+

Example 3 – MySQL TIMESTAMPADD() Function

To add a microsecond to the initial date using mysql timestampadd() function; as follows:

SELECT TIMESTAMPADD(MICROSECOND, 1, '1999-12-31');

Output-3

+--------------------------------------------+
| TIMESTAMPADD(MICROSECOND, 1, '1999-12-31') |
+--------------------------------------------+
| 1999-12-31 00:00:00.000001                 |
+--------------------------------------------+

Example 4 – MySQL TIMESTAMPADD() Function

To add a week to the initial date using the msyql timestampadd() function; as follows:

SELECT TIMESTAMPADD(WEEK,1,'2019-05-18'); 

Output-3

+--------------------------------------------+
| SELECT TIMESTAMPADD(WEEK,1,'2019-05-18')   |
+--------------------------------------------+
|  2019-05-25                                |
+--------------------------------------------+

Conclusion

MySQL TIMESTAMPADD() Function tutorial, you have learned how to use MySQL TIMESTAMPDIFF() function with various examples.

Leave a Comment