giovedì 23 gennaio 2025

SQL SERVER - arrotondare i DATETIME

In SQL Server, per arrotondare i DATETIME si utilizzano le funzioni DATEADD e DATEDIFF, in pratica si sottrae il tipo di valore che si vuole arrotondare a 0 (minuti, secondi, ore, etc).

Questo è un esempio:

DECLARE @dt DATETIME

SET @dt = '09-22-2007 15:07:38.850'

SELECT DATEADD(mi, DATEDIFF(mi, 0, @dt), 0)     -- ritorna 2007-09-22 15:07:00.000
SELECT DATEADD(HOUR, DATEDIFF(HOUR, 0, @dt), 0) -- ritorna 2007-09-22 15:00:00.000

Nessun commento:

Posta un commento