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

Detailed explanation of DOM DIFF algorithm in react application

Table of contents Preface What is VirtualDOM? Rea...

MySQL foreign key constraint (FOREIGN KEY) case explanation

MySQL foreign key constraint (FOREIGN KEY) is a s...

How to create a swap partition file in Linux

Introduction to Swap Swap (i.e. swap partition) i...

Summary of the differences between global objects in nodejs and browsers

In Node.js, a .js file is a complete scope (modul...

Installation tutorial of mysql5.7.21 decompression version under win10

Install the unzipped version of Mysql under win10...

MySQL transaction analysis

Transaction A transaction is a basic unit of busi...

Analysis of the reasons why MySQL's index system uses B+ tree

Table of contents 1. What is an index? 2. Why do ...

Example analysis of the principle and solution of MySQL sliding order problem

This article uses examples to explain the princip...

Example of how to build a Mysql cluster with docker

Docker basic instructions: Update Packages yum -y...

Pure CSS drop-down menu

Achieve results Implementation Code html <div ...

Vue component communication method case summary

Table of contents 1. Parent component passes valu...

Common naming rules for CSS classes and ids

Public name of the page: #wrapper - - The outer e...

How to use Docker container to access host network

Recently, a system was deployed, using nginx as a...

SQL Aggregation, Grouping, and Sorting

Table of contents 1. Aggregate Query 1. COUNT fun...

Solution to the problem of web page flash animation not displaying

<br />The solution steps are as follows: Sta...