TIMESTAMPDIFF

Sintassi

TIMESTAMPDIFF(unità, espr1, espr2)

Description:

Restituisce il risultato di espr2 - espr1, che sono entrambe di tipo DATETIME. Una espressione potrebbe essere una data e un'altra un datetime; in questo contesto, un valore DATE viene considerato come un DATETIME che ha come orario '00:00:00'. Il risultato è un intero e l'unità di misura è quella specificata dall'argomento unità. I valori ammessi sono gli stessi che si possono usare per la funzione TIMESTAMPADD().

TIMESTAMPDIFF può anche essere utilizzato per calcolare l'età.

Esempi

 
SELECT TIMESTAMPDIFF(MONTH,'2003-02-01','2003-05-01');
+------------------------------------------------+
| TIMESTAMPDIFF(MONTH,'2003-02-01','2003-05-01') |
+------------------------------------------------+
|                                              3 |
+------------------------------------------------+

SELECT TIMESTAMPDIFF(YEAR,'2002-05-01','2001-01-01');
+-----------------------------------------------+
| TIMESTAMPDIFF(YEAR,'2002-05-01','2001-01-01') |
+-----------------------------------------------+
|                                            -1 |
+-----------------------------------------------+

SELECT TIMESTAMPDIFF(MINUTE,'2003-02-01','2003-05-01 12:05:55');
+----------------------------------------------------------+
| TIMESTAMPDIFF(MINUTE,'2003-02-01','2003-05-01 12:05:55') |
+----------------------------------------------------------+
|                                                   128885 |
+----------------------------------------------------------+

Calcolare l'età:

SELECT name, date_of_birth, TIMESTAMPDIFF(YEAR,date_of_birth,'2014-08-02') AS age
FROM student_details;
+---------+---------------+------+ 
| name | date_of_birth | age | 
+---------+---------------+------+ 
| Chun | 1993-12-31 | 20 | 
| Esben | 1946-01-01 | 68 | 
| Kaolin | 1996-07-16 | 18 | 
| Tatiana | 1988-04-13 | 26 | 
+---------+---------------+------+ 

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.