by Olivier Moreau
19. septembre 2011 19:04
Voici un script SQL qui permet d'arrondir une date au quart d'heure le plus proche
DECLARE @MADATE AS DATETIME
SET @MADATE = '01/01/2011 10:07:29'
SELECT cast(round((cast(@MADATE as float(53))*24*4),0)/(24*4) as smalldatetime)
Résultat : 2011-01-01 10:00:00
SET @MADATE = '01/01/2011 10:07:30'
SELECT cast(round((cast(@MADATE as float(53))*24*4),0)/(24*4) as smalldatetime)
Résultat : 2011-01-01 10:15:00
Pour arrondir systématiquement au quart d'heure inférieur on utilisera la fonction FLOOR à la place de ROUND
DECLARE @MADATE AS DATETIME SET @MADATE = '01/01/2011 10:07:29'
SELECT cast(floor(cast(@MADATE as float(53))*24*4)/(24*4) as smalldatetime)
Résultat : 2011-01-01 10:00:00
SET @MADATE = '01/01/2011 10:14:30'
SELECT cast(floor(cast(@MADATE as float(53))*24*4)/(24*4) as smalldatetime)
Résultat : 2011-01-01 10:00:00