Funzione DATEDIFF() di SQL Server
Definizione e uso
La funzione DATEDIFF() restituisce il tempo tra due date.
Sintassi
DATEDIFF(datepart,startdate,enddate)
startdate e enddate Il parametro è un'espressione di data valida.
datepart Il parametro può essere uno dei seguenti valori:
datepart | Abbreviazioni |
---|---|
Anni | yy, yyyy |
Trimestri | qq, q |
Mesi | mm, m |
Giorno dell'anno | dy, y |
Giorni | dd, d |
Settimane | wk, ww |
Settimane | dw, w |
Ore | hh |
Minuti | mi, n |
Secondi | ss, s |
Millisecondi | ms |
Microsecondi | mcs |
Nanosecondi | ns |
Esempio
Esempio 1
Usare la seguente istruzione SELECT:
SELECT DATEDIFF(day,'2008-12-29','2008-12-30') AS DiffDate
Risultato:
DiffDate |
---|
1 |
Esempio 2
Usare la seguente istruzione SELECT:
SELECT DATEDIFF(day,'2008-12-30','2008-12-29') AS DiffDate
Risultato:
DiffDate |
---|
-1 |