MySQL TIMESTAMPADD() Function

MySQL TIMESTAMPADD() Function

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.

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *