DATE_ADD
Sintassi
DATE_ADD(data, INTERVAL espr unità), DATE_SUB(data, INTERVAL espr unità)
Spiegazione
Queste funzioni effettuando un calcolo aritmetico sulle date. L'argomento data specifica il valore iniziale di tipo date o datetime. espr è un'espressione che specifica l'intervallo da aggiungere o sottrarre alla data iniziale. espr è una stringa; può iniziare con "-
" per gli intervalli negativi. unità rappresenta una parola riservata che indica l'unità di misura delle relative espressioni.
Esempi
MariaDB [(none)]> SELECT '2008-12-31 23:59:59' + INTERVAL 1 SECOND; +-------------------------------------------+ | '2008-12-31 23:59:59' + INTERVAL 1 SECOND | +-------------------------------------------+ | 2009-01-01 00:00:00 | +-------------------------------------------+ 1 row in set (0.00 sec)
MariaDB [(none)]> SELECT INTERVAL 1 DAY + '2008-12-31'; +-------------------------------+ | INTERVAL 1 DAY + '2008-12-31' | +-------------------------------+ | 2009-01-01 | +-------------------------------+ 1 row in set (0.00 sec)
MariaDB [(none)]> SELECT '2005-01-01' - INTERVAL 1 SECOND; +----------------------------------+ | '2005-01-01' - INTERVAL 1 SECOND | +----------------------------------+ | 2004-12-31 23:59:59 | +----------------------------------+ 1 row in set (0.00 sec)
MariaDB [(none)]> SELECT DATE_ADD('2000-12-31 23:59:59', -> INTERVAL 1 SECOND); +----------------------------------------------------+ | DATE_ADD('2000-12-31 23:59:59', INTERVAL 1 SECOND) | +----------------------------------------------------+ | 2001-01-01 00:00:00 | +----------------------------------------------------+ 1 row in set (0.00 sec)
MariaDB [(none)]> SELECT DATE_ADD('2010-12-31 23:59:59', -> INTERVAL 1 DAY); +-------------------------------------------------+ | DATE_ADD('2010-12-31 23:59:59', INTERVAL 1 DAY) | +-------------------------------------------------+ | 2011-01-01 23:59:59 | +-------------------------------------------------+ 1 row in set (0.00 sec)
MariaDB [(none)]> SELECT DATE_ADD('2100-12-31 23:59:59', -> INTERVAL '1:1' MINUTE_SECOND); +---------------------------------------------------------------+ | DATE_ADD('2100-12-31 23:59:59', INTERVAL '1:1' MINUTE_SECOND) | +---------------------------------------------------------------+ | 2101-01-01 00:01:00 | +---------------------------------------------------------------+ 1 row in set (0.00 sec)
MariaDB [(none)]> SELECT DATE_SUB('2005-01-01 00:00:00', -> INTERVAL '1 1:1:1' DAY_SECOND); +----------------------------------------------------------------+ | DATE_SUB('2005-01-01 00:00:00', INTERVAL '1 1:1:1' DAY_SECOND) | +----------------------------------------------------------------+ | 2004-12-30 22:58:59 | +----------------------------------------------------------------+ 1 row in set (0.00 sec)
MariaDB [(none)]> SELECT DATE_ADD('1900-01-01 00:00:00', -> INTERVAL '-1 10' DAY_HOUR); +------------------------------------------------------------+ | DATE_ADD('1900-01-01 00:00:00', INTERVAL '-1 10' DAY_HOUR) | +------------------------------------------------------------+ | 1899-12-30 14:00:00 | +------------------------------------------------------------+ 1 row in set (0.00 sec)
MariaDB [(none)]> SELECT DATE_SUB('1998-01-02', INTERVAL 31 DAY); +-----------------------------------------+ | DATE_SUB('1998-01-02', INTERVAL 31 DAY) | +-----------------------------------------+ | 1997-12-02 | +-----------------------------------------+ 1 row in set (0.00 sec)
MariaDB [(none)]> SELECT DATE_ADD('1992-12-31 23:59:59.000002', -> INTERVAL '1.999999' SECOND_MICROSECOND); +--------------------------------------------------------------------------------+ | DATE_ADD('1992-12-31 23:59:59.000002', INTERVAL '1.999999' SECOND_MICROSECOND) | +--------------------------------------------------------------------------------+ | 1993-01-01 00:00:01.000001 | +--------------------------------------------------------------------------------+ 1 row in set (0.00 sec) MariaDB [(none)]>
Commenti
Sto caricando i commenti......
Content reproduced on this site is the property of its respective owners,
and this content is not reviewed in advance by MariaDB. The views, information and opinions
expressed by this content do not necessarily represent those of MariaDB or any other party.