ADDTIME() Function Examples – MySQL

ADDTIME() Function Examples – MySQL

In this MySQL Tutorial, we are going to demonstrate to you how to use addtime() function with different examples.

MySQL ADDTIME() Function

MySQL ADDTIME() works to add a specified amount of time or DateTime expression.

Syntax

The addtime() function basic syntax is:

ADDTIME(first,second)

The addtime() function required two parameters. First, param is the original date/time value, and the second param is the amount of time you want to add to it.

MySQL ADDTIME Function Examples

The following are list examples that help you understand the use of this MySQL ADDTIME function.

Example-1 Basic Usage

Let’s take the first example of addtime() :

SELECT ADDTIME('01:00:00', '03:40:00') AS Result;

Output-1

+----------+
| Result   |
+----------+
| 04:40:00 |
+----------+

So the first param is increased by the amount of the second param.

Example-2

We take the next example of addtime() with MySQL NOW().

SELECT ADDTIME(now(), '02:00:00') AS result

Output-2

+----------+
| Result   |
+----------+
| 12:40:00 |
+----------+

Using addtime(), we have added 2 hours in the current time.

Example-3 Fractional Seconds

The time value can have a fractional seconds part if required:

SELECT ADDTIME('01:00:00.000000', '03:40:00.123456') AS Result;

Output-3

+-----------------+
| Result          |
+-----------------+
| 04:40:00.123456 |
+-----------------+

Example-4 With DateTime

Using the below MySQL addtime() example, you can also use it to increment date values:

SELECT ADDTIME('2019-11-01 00:00:00', '10 04:35:59') AS Result;

Output-4

+---------------------+
| Result              |
+---------------------+
| 2019-11-11 04:35:59 |
+---------------------+

So, in this case, we increased the day component as well as the time component.

Example-5 MySQL Database

Let’s take another example with the MySQL database. We will add 5 hours in current time and fetch employees data from database table employees.

 SELECT * 
 FROM employees 
 WHERE  start_time > ADDTIME(now(), '05:00:00')

Conclusion

Here, you have learned how to use MySQL ADDTIME() function with various examples.

You may like

  1. TIMESTAMP() Function Examples – MySQL
  2. TIMEDIFF() Function Examples – MySQL
  3. SEC_TO_TIME() Function Examples – MySQL
  4. QUARTER() Function Examples – MySQL
  5. STR_TO_DATE() Function Examples – MySQL
  6. PERIOD_DIFF() Function Examples – MySQL
  7. PERIOD_ADD() Function Examples – MySQL
  8. SECOND() Function Examples – MySQL
  9. MICROSECOND() Function Examples – MySQL
  10. MAKETIME() Function Examples – MySQL
  11. Localtime() Function Examples – MySQL
  12. CURTIME() Function Examples – MySQL
  13. MySQL CURRENT TIMESTAMP Function Examples
  14. MySQL CURRENT TIME Function Examples
  15. MINUTE() Function Examples – MySQL
  16. HOUR() Function Examples – MySQL
  17. NOW() MySQL Function | MySQL
  18. MySQL DATE() And TIME() Functions (List)
  19. TIMESTAMPADD() Function Examples – MySQL
  20. TIMESTAMPDIFF() Function Examples – MySQL

If you have any questions or thoughts to share, use the comment form below to reach us.

Leave a Reply

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