mirror of
https://github.com/jbranchaud/til
synced 2026-01-02 22:58:01 +00:00
Add Doing Date Math as a mysql til
This commit is contained in:
@@ -10,7 +10,7 @@ smart people at [Hashrocket](http://hashrocket.com/).
|
||||
For a steady stream of TILs from a variety of rocketeers, checkout
|
||||
[til.hashrocket.com](https://til.hashrocket.com/).
|
||||
|
||||
_709 TILs and counting..._
|
||||
_710 TILs and counting..._
|
||||
|
||||
---
|
||||
|
||||
@@ -307,6 +307,7 @@ _709 TILs and counting..._
|
||||
### MySQL
|
||||
|
||||
- [Display Output In A Vertical Format](mysql/display-output-in-a-vertical-format.md)
|
||||
- [Doing Date Math](mysql/doing-date-math.md)
|
||||
- [Dump A Database To A File](mysql/dump-a-database-to-a-file.md)
|
||||
- [List Databases And Tables](mysql/list-databases-and-tables.md)
|
||||
- [Show Create Statement For A Table](mysql/show-create-statement-for-a-table.md)
|
||||
|
||||
31
mysql/doing-date-math.md
Normal file
31
mysql/doing-date-math.md
Normal file
@@ -0,0 +1,31 @@
|
||||
# Doing Date Math
|
||||
|
||||
MySQL has an array of functions for interacting with `date` and `datetime`
|
||||
values. If you'd like to do math with a date to compute a date in the future
|
||||
or the past, you can use the
|
||||
[`DATE_ADD()`](https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-add)
|
||||
and
|
||||
[`DATE_SUB()`](https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-sub)
|
||||
functions.
|
||||
|
||||
```mysql
|
||||
mysql> select now() Now, date_add(now(), interval 10 minute) '10 Minutes Later';
|
||||
+---------------------+---------------------+
|
||||
| Now | 10 Minutes Later |
|
||||
+---------------------+---------------------+
|
||||
| 2018-10-18 15:53:29 | 2018-10-18 16:03:29 |
|
||||
+---------------------+---------------------+
|
||||
|
||||
mysql> select now() Now, date_sub(now(), interval 9 day) '9 Days Earlier';
|
||||
+---------------------+---------------------+
|
||||
| Now | 9 Days Earlier |
|
||||
+---------------------+---------------------+
|
||||
| 2018-10-18 15:54:01 | 2018-10-09 15:54:01 |
|
||||
+---------------------+---------------------+
|
||||
```
|
||||
|
||||
There are equivalent `ADDDATE()` and `SUBDATE()` functions if you prefer.
|
||||
|
||||
Check out [the
|
||||
docs](https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html)
|
||||
for more details.
|
||||
Reference in New Issue
Block a user