MySQL DATE_ADD and ADDDATE functions add a specified time interval to a date

MySQL DATE_ADD and ADDDATE functions add a specified time interval to a date

MySQL DATE_ADD(date,INTERVAL expr type) and ADDDATE(date,INTERVAL expr type) functions have the same function, both are used to perform date addition operations.

The DATE_ADD() and ADDDATE() functions have two parameters:

  • date is the starting DATE or DATETIME value.
  • INTERVAL expr type is the interval value to be added to the start date value.

[Example 1] Use the DATE_ADD(date,INTERVAL expr type) function to perform date addition operations. The input SQL statement and the execution result are as follows.

mysql> SELECT DATE_ADD('2018-10-31 23:59:59',INTERVAL 1 SECOND) AS C1,
  -> DATE_ADD('2018-10-31 23:59:59',INTERVAL '1:1' MINUTE_SECOND) AS C2,
  -> ADDDATE('2018-10-31 23:59:59',INTERVAL 1 SECOND) AS C3;
+---------------------+---------------------+---------------------+
| C1 | C2 | C3 |
+---------------------+---------------------+---------------------+
| 2018-11-01 00:00:00 | 2018-11-01 00:01:00 | 2018-11-01 00:00:00 |
+---------------------+---------------------+---------------------+
1 row in set (0.00 sec)

From the execution results, we can see that the functions of DATE_ADD(date,INTERVAL expr type) and ADDDATE(date,INTERVAL expr type) are exactly the same. After adding 1 second to the original time '2018-10-31 23:59:59', the result is '2018-11-01 00:00:00'; adding 1 minute and 1 second to the original time is the expression '1:1', and the final result is '2018-11-01 00:01:00'.

[Example 2] Use the ADDDATE() function to perform date addition operations. The input SQL statement and execution results are shown below.

mysql> SELECT ADDDATE('2017-11-30 23:59:59', INTERVAL 1 SECOND) AS col1,
  -> ADDDATE('2017-11-30 23:59:59' ,INTERVAL '1:1' MINUTE_SECOND) AS col2;
+---------------------+---------------------+
| col1 | col2 |
+---------------------+---------------------+
| 2017-12-01 00:00:00 | 2017-12-01 00:01:00 |
+---------------------+---------------------+
1 row in set (0.02 sec)

From the running results, we can see that the result of the ADDDATE('2017-11-30 23:59:59', INTERVAL 1 SECOND) function execution increases the time by 1 second and returns the result as "2017-12-01 00:00:00"; the date operation type of the ADDDATE('2017-11-30 23:59:59', INTERVAL'1:1'MINUTE_SECOND) function is MINUTE_SECOND, which increases the specified time by 1 minute and 1 second and returns the result as "2017-12-01 00:01:00".

This is the end of this article about MySQL DATE_ADD and ADDDATE functions to add specified time intervals to dates. For more information about MySQL specified time intervals, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • MySQL spatial data storage and functions
  • Detailed explanation of single-row function code of date type in MySQL
  • MySql sharing of null function usage
  • Common functions of MySQL basics
  • Detailed explanation of Mysql function call optimization
  • Example tutorial on using the sum function in MySQL
  • How to use MySQL common functions to process JSON
  • A brief introduction to MySQL functions

<<:  Application of HTML and CSS in Flash

>>:  Detailed explanation of CSS counter related attributes learning

Recommend

Sample code for deploying ELK using Docker-compose

environment Host IP 192.168.0.9 Docker version 19...

Analysis and solution of a.getAttribute(href,2) problem in IE6/7

Brief description <br />In IE6 and 7, in a ...

5 Commands to Use the Calculator in Linux Command Line

Hello everyone, I am Liang Xu. When using Linux, ...

How to implement line breaks in textarea text input area

If you want to wrap the text in the textarea input...

Linux CentOS MySQL database installation and configuration tutorial

Notes on installing MySQL database, share with ev...

MySQL Series 4 SQL Syntax

Table of contents Tutorial Series 1. Introduction...

Solve the problem that the time zone cannot be set in Linux environment

When changing the time zone under Linux, it is al...

Tutorial on deploying the open source project Tcloud with Docker on CentOS8

1. Install Docker 1. I installed Centos7 in the v...

MySQL column to row conversion tips (share)

Preface: Because many business tables use design ...

Example of how to deploy MySQL 8.0 using Docker

1. Refer to the official website to install docke...

MySQL 8.0.21 installation tutorial under Windows system (illustration and text)

Installation suggestion : Try not to use .exe for...

Some indicators of excellent web front-end design

The accessibility of web pages seems to be somethi...