Mysql Date Arithmetic
Date Arithmetic is used to display the current date, interval in current date and time.
Understand with Example
The Tutorial illustrate an example from 'Date Arithmetic' in Mysql. To grasp this example, we run the select now ( ) query that return you the current date and time.
SYNTAX:-date + INTERVAL expression unit
Query:-
For current date and time
mysql> Select now(); |
Output:-
+---------------------+ | now() | +---------------------+ | 2008-12-19 14:34:02 | +---------------------+ 1 row in set (0.02 sec) |
Query:-
The Query returns you the 2 days and time before the current time
For subtracting 2 days from current date.
mysql> select now()- interval 2 day; |
Output:-
+-----------------------+ | now()- interval 2 day | +-----------------------+ | 2008-12-17 14:34:47 | +-----------------------+ 1 row in set (0.02 sec) |
Query:-
The Query returns you the 5 minute before the current minute.
For current date+time minus 5 minutes
mysql> select now()- interval 5 minute; |
Output:-
+--------------------------+ | now()- interval 5 minute | +--------------------------+ | 2008-12-19 14:35:13 | +--------------------------+ 1 row in set (0.00 sec) |
Query:-
The Query adds 5 days to the current date.
For Adding 5 days from current date.
mysql> select now()+ interval 5 day; |
Output:-
+-----------------------+ | now()+ interval 5 day | +-----------------------+ | 2008-12-24 14:45:06 | +-----------------------+ 1 row in set (0.00 sec) |