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 |
AskSQLTeam
Ask SQLTeam Question
0 Posts |
Posted - 2006-11-29 : 08:07:25
|
David Mercer writes "I am trying to work out a function that will allow us to round a given time to the nearest custom interval.I work in a hospital where we have consultant clinics. Take the example of a typical clinic where the session runs from 09:00 to 12:00 with each appointment lasting 30mins.So to fit there must be an appointments starting at the following times09:0009:3010:0010:3011:0011:30The problem is that within our data we have appointments for example at 10:05 and we want that time to say 10:30 instead which is the next appointment time after 10:05.I have come up with a script that converts the times into seconds past midnight and does some rounding but then I get a bit stuck.declare @starttime as intdeclare @slottime as intdeclare @appttime as intdeclare @newtime as intset @starttime = (cast(substring('12:05',1,2) as int) * 3600) + (cast(substring('12:05',4,2) as int) * 60)set @appttime = (cast(substring('12:06',1,2) as int) * 3600) + (cast(substring('12:06',4,2) as int) * 60)set @slottime = 5set @newtime = @starttime + round(@starttime/@appttime,0) + @slottimeprint @newtime select 3600 * 6print 43505 / 3600print ((cast(43505 as decimal(10,0))/cast(3600 as decimal(10,0)))-round((43505/3600),0)) * 60Can anyone suggest a script that will help me here?David" |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-11-29 : 08:51:33
|
[code]-- prepare test datadeclare @test table (timeinfo datetime)insert @testselect '10:05' union allselect '11:17' union allselect '12:30' union allselect '09:15' union allselect '10:00' union allselect '10:59'declare @intervals table (minutes tinyint)insert @intervalsselect 5 union allselect 10 union allselect 15 union allselect 30 union allselect 45 union allselect 60 union allselect 90-- do the workselect t.timeinfo, i.minutes interval, DATEADD(minute, (i.minutes - datediff(minute, 0, t.timeinfo) % i.minutes) % i.minutes, t.timeinfo) [nearest future time]from @test tcross join @intervals iorder by i.minutes, t.timeinfo [/code]Peter LarssonHelsingborg, Sweden |
|
|
|
|
|
|
|