Timestampadd() function in MySQL; In this tutorial, we will learn how to add the specified time of given date or DateTime value using MySQL TIMESTAMPADD() with the help of examples.
MySQL TIMESTAMPADD() Function
In MySQL, the TIMESTAMPADD () function allows you to add a specified amount to a date or a DateTime value.
Syntax
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.
We provide the unit argument see below:
MICROSECOND
-
SECOND
-
MINUTE
HOUR
DAY
WEEK
MONTH
QUARTER
YEAR
Example-1
In this example, we will add a day to the initial date.
SELECT TIMESTAMPADD(DAY, 1, '1999-12-31');
Output-1
+------------------------------------+ | TIMESTAMPADD(DAY, 1, '1999-12-31') | +------------------------------------+ | 2000-01-01 | +------------------------------------+
Example-2
In this example, we will add a second to the initial date.
SELECT TIMESTAMPADD(SECOND, 1, '1999-12-31');
Output-2
+---------------------------------------+ | TIMESTAMPADD(SECOND, 1, '1999-12-31') | +---------------------------------------+ | 1999-12-31 00:00:01 | +---------------------------------------+
The result is now a DateTime value in order to return the seconds part.
Example-3
In this example, we will add a microsecond to the initial date.
SELECT TIMESTAMPADD(MICROSECOND, 1, '1999-12-31');
Output-3
+--------------------------------------------+ | TIMESTAMPADD(MICROSECOND, 1, '1999-12-31') | +--------------------------------------------+ | 1999-12-31 00:00:00.000001 | +--------------------------------------------+
Example-4
In this example, we will add a week to the initial date.
SELECT TIMESTAMPADD(WEEK,1,'2019-05-18');
Output-3
+--------------------------------------------+ | SELECT TIMESTAMPADD(WEEK,1,'2019-05-18') | +--------------------------------------------+ | 2019-05-25 | +--------------------------------------------+
Conclusion
Here, you have learned how to use MySQL TIMESTAMPDIFF() function with various examples.
Recommended MySQL Tutorials
If you have any questions or thoughts to share, use the comment form below to reach us.