⌘BABAKOV.NET Let's code!
100% [light] [ru]

Description of the ADDDATE function in MySQL (with time interval types)

The ADDDATE function returns the input time specified by the first parameter, with the added time interval specified by the second parameter.

Syntax:

ADDDATE(date, INTERVAL expr type)


The ADDDATE() function has a synonym: DATE_ADD().

The second parameter of the function starts with the keyword INTERVAL, followed by the value and type of this value.

The function also allows the use of negative values, in which case the period is subtracted, not added.

! There is a simplified format for writing the ADDDATE(date,days) function - where days is the number of days.

Example of using the function:

mysql> select adddate(\'2017-03-29\', interval 15 day);
+----------------------------------------+
| adddate(\'2017-03-29\', interval 15 day) |
+----------------------------------------+
| 2017-04-13                             |
+----------------------------------------+


MICROSECOND - Microseconds
Format: xxxxxx

SECOND - Seconds
Format: ss

MINUTE - Minutes
Format: mm

HOUR - Hours
Format: hh

DAY - Days
Format: DD

WEEK - Weeks
Format: WW

MONTH - Months
Format: MM

QUATER - Quarters
Format: QQ

YEAR - Year
Format: YY

SECOND_MICROSECOND - Seconds and microseconds
Format: \ss.xxxxxx\'

MINUTE_MICROSECOND - Minutes, seconds and microseconds
Format: \mm:ss.xxxxxx\'

MINUTE_SECOND - Minutes and seconds
Format: \mm:ss\'

HOUR_MICROSECOND - Hours, minutes, seconds and microseconds
Format: \hh:mm:ss.xxxxxx\'

HOUR_SECOND - Hours, minutes and seconds
Format: \hh:mm:ss\'

HOUR_MINUTE - Hours and minutes
Format: \hh:mm\'

DAY_MICROSECOND - Days, hours, minutes, seconds and microseconds
Format: \DD hh:mm:ss.xxxxxx\'

DAY_SECOND - Days, hours, minutes and seconds
Format: \DD hh:mm:ss\'

DAY_MINUTE - Days, hours and minutes
Format: \DD hh:mm\'

DAY_HOUR - Days and hours
Format: \DD hh\'

YEAR_MONTH - Years and months
Format: \YY-MM\'


Example of using ADDDATE with adding milliseconds (MICROSECOND):

mysql> select adddate(\'2017-03-29\', interval 15 microsecond);
+------------------------------------------------+
| adddate(\'2017-03-29\', interval 15 microsecond) |
+------------------------------------------------+
| 2017-03-29 00:00:00.000015                     |
+------------------------------------------------+



Example of ADDDATE using the type (MINUTE_MICROSECOND):

mysql> select adddate(\'2017-03-29\', interval \'29:10.25\' minute_microsecond);
+---------------------------------------------------------------+
| adddate(\'2017-03-29\', interval \'29:10.25\' minute_microsecond) |
+---------------------------------------------------------------+
| 2017-03-29 00:29:10.250000                                    |
+---------------------------------------------------------------+



Example of ADDDATE using the type (WEEK):

mysql> select adddate(\'2017-03-29\', interval 34 week);
+-----------------------------------------+
| adddate(\'2017-03-29\', interval 34 week) |
+-----------------------------------------+
| 2017-11-22                              |
+-----------------------------------------+



Example of using the simplified variant of the function with a negative value:

mysql> select adddate(\'2017-03-29\', -12);
+----------------------------+
| adddate(\'2017-03-29\', -12) |
+----------------------------+
| 2017-03-17                 |
+----------------------------+



That's all!

Questions and comments are welcome.

Comments (0)

No comments yet.

Leave a comment
Comment
Images (up to 5 MB)
Your comment will appear after moderation.