If you are expecting your result to be
10:05
then you actually only want times that are divisible by 5 - you are currently always rounding up to the
next number divisible by 5.
I would probably go about it differently - first check if the time is already one you want - i.e. if you divide by 5 is there a remainder. If not then calculate how many minutes need to be added - again by using the remainder when dividing by 5, and just add those minutes using a standard SQL function
DATEADD()
For example:
DECLARE @Time AS TIME='10:05'
DECLARE @RoundToMin AS INT=5
DECLARE @MIN AS INT
DECLARE @FinalTime AS DATETIME
SET @MIN = Datepart(n, @Time)
IF (@MIN % @RoundToMin) > 0
SET @FinalTime = DATEADD(MINUTE, @RoundToMin - (@MIN % @RoundToMin), @Time)
else
SET @FinalTime = @Time
SELECT @FinalTime
This will return
January, 01 1900 10:05
for your input of 10:05 and
January, 01 1900 10:10
for an input of 10:06
However, watch what happens if you put in @Time = 23:59... you get
January, 01 1900 00:00:00+0000
... is that correct or do you want it to clock over to the next "day"?
If you do want to determine that you've gone over a day threshold, then change the declaration of
@Time
to
DECLARE @Time AS DATETIME='23:59'
which will return
January, 02 1900 00:00:00+0000