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

A few things you need to know about responsive layout

1. Introduction Responsive Web design allows a we...

JDBC Exploration SQLException Analysis

1. Overview of SQLException When an error occurs ...

Problems encountered in using MySQL

Here are some problems encountered in the use of ...

Practice of deploying web applications written in Python with Docker

Table of contents 1. Install Docker 2. Write code...

JavaScript to achieve all or reverse selection function

This article shares the specific code of JavaScri...

Summary of Linux Logical Volume Management (LVM) usage

Managing disk space is an important daily task fo...

Vue two fields joint verification to achieve the password modification function

Table of contents 1. Introduction 2. Solution Imp...

How to use JSX to implement Carousel components (front-end componentization)

Before we use JSX to build a component system, le...

In-depth explanation of modes and environment variables in Vue CLI

Preface In the development of actual projects, we...

How to view and set the mysql time zone

1. Check the database time zone show variables li...

Summary of Node.js service Docker container application practice

This article will not explain the use and install...

MySQL 8.0.11 installation and configuration method graphic tutorial (win10)

This article records the installation and configu...