Hello:

I've searched quite a bit and found a few solutions but not anything solid that currently works. What I'm trying to do is basically round the time part to the nearest 30 minute, hour, etc...as displayed below:

Rounding the same as numbers such as if rounding to the nearest 10th, 4.9 is 0, 5 is 10, 12.2 is 10, etc...Same concept with time.

ITEMS LOOKING FOR: (the date should not be considered at all, will be removed, no problem on this)

1) Round to the nearest Half Hour

2) Round to the nearest Hour

* 3) Round to the nearest 2nd Hour in a 24-Hour period

** 4) Round to the nearest 4th Hour in a 24-Hour period

*** 5) Round to the nearest 6th Hour in a 24-Hour period

**** 6) Round to the nearest 12th Hour in a 24-Hour period

* 2ND HOUR - 24-Hour period would be 12:00 AM, 2:00 AM, 4:00 AM, etc...all the way to 10:00 PM being the last 2-Hour period.

** 4TH HOUR - 24-Hour period would be 12:00 AM, 4:00 AM, 8:00 AM, etc...all the way to 8:00 PM being the last 4-Hour period.

*** 6TH HOUR - 24-Hour period would be 12:00 AM, 6:00 AM, 12:00 PM, and 6:00, all being the complete 6-Hour period.

**** 12TH HOUR - 24-Hour period would be 12:00 AM and 12:00 PM, both being the complete 12-Hour period.

I've searched quite a bit and found a few solutions but not anything solid that currently works. What I'm trying to do is basically round the time part to the nearest 30 minute, hour, etc...as displayed below:

Rounding the same as numbers such as if rounding to the nearest 10th, 4.9 is 0, 5 is 10, 12.2 is 10, etc...Same concept with time.

ITEMS LOOKING FOR: (the date should not be considered at all, will be removed, no problem on this)

1) Round to the nearest Half Hour

2) Round to the nearest Hour

* 3) Round to the nearest 2nd Hour in a 24-Hour period

** 4) Round to the nearest 4th Hour in a 24-Hour period

*** 5) Round to the nearest 6th Hour in a 24-Hour period

**** 6) Round to the nearest 12th Hour in a 24-Hour period

* 2ND HOUR - 24-Hour period would be 12:00 AM, 2:00 AM, 4:00 AM, etc...all the way to 10:00 PM being the last 2-Hour period.

** 4TH HOUR - 24-Hour period would be 12:00 AM, 4:00 AM, 8:00 AM, etc...all the way to 8:00 PM being the last 4-Hour period.

*** 6TH HOUR - 24-Hour period would be 12:00 AM, 6:00 AM, 12:00 PM, and 6:00, all being the complete 6-Hour period.

**** 12TH HOUR - 24-Hour period would be 12:00 AM and 12:00 PM, both being the complete 12-Hour period.

datepart (arg, date_col) will return a part of a date according to arg

Datepart Abbreviations

to get the year use yy or yyyy

to get the month use mm or m

to get the day use dd or d

to get the hour use hh

to get the minute use mi or n

to get the second use ss or s

in order to assemble the date back, you just use the same function, and concat, for example

select cast( char(years)+'.'+char(month

the format depends on the format of dates that you use

this format is always acceptable

yyyymmdd

also consider using the dateadd function

i did not really understand the logic by which you do the rounding, so i'll assume that if x<5 then x is rounded to 0, otherwise it is rounded to 10

assuming m holds the minutes, and h hold the hours

round to 30m :

if m < 15 then

begin

set @m = 0

end

else if m >= 15 and m < 45

begin

set @m = 30

end

else if m >= 45

begin

set @m = 0

set @h = @h + 1

end

something ilke that is ok ?

```
CREATE function [dbo].[RoundTime] (@Time datetime, @RoundTo float)
returns datetime
as
begin
declare @RoundedTime smalldatetime
declare @Multiplier float
set @Multiplier= 24.0/@RoundTo
set @RoundedTime= ROUND(cast(@Time as float) * @Multiplier,0)/@Multiplier
return @RoundedTime
end
```

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy

Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif

Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster

CTP, Sr Infrastructure Consultant

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Connect with Certified Experts to gain insight and support on specific technology challenges including:

- Troubleshooting
- Research
- Professional Opinions

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.