Click here to Skip to main content
14,767,338 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Good Day
I want to round off time to lowest and highest time up to 5 minutes
if i have time say

09:03 if it is lowest it will round off to 09:00
And if it is highest it will round off to 09:05

Can anyone help me out
Posted

1 solution

DECLARE @MIN AS INT
SET @MIN = DATEPART(N, GETDATE())

SELECT (@MIN / 5) * 5 AS LOW, ((@MIN / 5) * 5) + 5 AS HI

Put your date in place of GETDATE()...
   
v2
Comments
surajemo 27-May-14 8:09am
   
Thanks you for replying
let me try it out
surajemo 27-May-14 8:21am
   
Kornfeld Eliyahu Peter :with the help of your solution i did it like this
declare @Roundoffvalue as int =5
declare @fromtime as time(7)='09:03'
declare @Hours as int;
set @Hours =(select datepart(HH,@fromtime))
DECLARE @MIN AS INT
SET @MIN = DATEPART(N, @fromtime)

SELECT @Hours '@Hours',

Convert(time(7),Convert(varchar(90),@Hours)+':'+Convert(varchar(90),Convert(varchar(90),(@MIN / @Roundoffvalue) * @Roundoffvalue) ))AS LOW,
Convert(time(7),Convert(varchar(90),@Hours)+':'+convert(varchar(90), ((@MIN / @Roundoffvalue) * @Roundoffvalue) + @Roundoffvalue)) AS HI


Can this be done in a more better way .
Kornfeld Eliyahu Peter 27-May-14 8:33am
   
You may separate computations from select, as now you have the same computations repeatedly...
surajemo 27-May-14 9:05am
   
You mean the hours part right

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900