Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
JimL
SQL Slinging Yak Ranger
1537 Posts |
Posted - 2004-12-14 : 09:12:31
|
| Anyone have a easy method to round a datetime to the nearest hour?JimUsers <> Logic |
|
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2004-12-14 : 09:15:02
|
| Same as rounding to any other interval:SELECT DateAdd(hour, DateDiff(hour, 0, getdate()), 0)The only intervals that don't work with that formula are seconds and milliseconds, because they will overflow an int value. |
 |
|
|
JimL
SQL Slinging Yak Ranger
1537 Posts |
Posted - 2004-12-14 : 10:05:46
|
| Thanks Rob I used it to create a table of half hour increments between two timesI just needed to start at an even hour.Declare @TimeIn DatetimeDeclare @Timeout DatetimeSELECT @TimeIn = DateAdd(hour, DateDiff(hour, 0, TimeIn), 0) , @Timeout = DateAdd(hour, DateDiff(hour, 0, Timeout), 0)FROM TimeClockPlus.dbo.EmployeeHoursWHERE (RecordId = @RecordId)Select DateAdd(mi,30, @TimeIn) As AvailInInto #AvailTimeSelect @TimeIn = DateAdd(mi,60, @TimeIn)While @TimeIn < @TimeoutBeginInsert into #AvailTime(AvailIn)Select @TimeInSelect @TimeIn = DateAdd(mi,30, @TimeIn)endSelect AvailInFrom #AvailTimeOrder by AvailInJimUsers <> Logic |
 |
|
|
|
|
|
|
|