Please start any new threads on our new site at http://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

 SQL Server Forums Profile | Active Topics | Members | Search | Forum FAQ Register Now and get your question answered!
 All Forums  SQL Server 2000 Forums  SQL Server Development (2000)  Rouding time values up to the nearest custon interval Reply to Topic  Printer Friendly
Author  Topic

USA
0 Posts

 Posted - 11/29/2006 :  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

Sweden
30421 Posts

 Posted - 11/29/2006 :  08:51:33 ```-- prepare test data declare @test table (timeinfo datetime) insert @test select '10:05' union all select '11:17' union all select '12:30' union all select '09:15' union all select '10:00' union all select '10:59' declare @intervals table (minutes tinyint) insert @intervals select 5 union all select 10 union all select 15 union all select 30 union all select 45 union all select 60 union all select 90 -- do the work select 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 t cross join @intervals i order by i.minutes, t.timeinfo ```Peter LarssonHelsingborg, Sweden Edited by - SwePeso on 11/29/2006 09:04:01
Topic
 Reply to Topic  Printer Friendly Jump To: Select Forum General SQL Server Forums       New to SQL Server Programming       New to SQL Server Administration       Script Library       Data Corruption Issues       Database Design and Application Architecture SQL Server 2012 Forums       Transact-SQL (2012)       SQL Server Administration (2012)       SSIS and Import/Export (2012)       Analysis Server and Reporting Services (2012)       Replication (2012)       Availability Groups and DR (2012)       Other SQL Server 2012 Topics SQL Server 2008 Forums       Transact-SQL (2008)       SQL Server Administration (2008)       SSIS and Import/Export (2008)       High Availability (2008)       Replication (2008)       Analysis Server and Reporting Services (2008)       Other SQL Server 2008 Topics SQL Server 2005 Forums       Transact-SQL (2005)       SQL Server Administration (2005)       .NET Inside SQL Server (2005)       SSIS and Import/Export (2005)       Service Broker (2005)       Replication (2005)       High Availability (2005)       Analysis Server and Reporting Services (2005)       Express Edition and Compact Edition (2005)       Other SQL Server Topics (2005) SQL Server 2000 Forums       SQL Server Development (2000)       SQL Server Administration (2000)       Import/Export (DTS) and Replication (2000)       Transact-SQL (2000)       Analysis Services (2000)       MSDE (2000) Development Tools       ASP.NET       Reporting Services Development       Other Development Tools Site Related Forums       Site Related Discussions       Article Discussion       Poll Discussion       The Yak Corral Other Forums       SQL Server 6.5 \ SQL Server 7.0       Other Topics       MS Access       ClearTrace Support Forum Old Forums       CLOSED - General SQL Server       CLOSED - SQL Server 2005/Yukon  -------------------- Home Active Topics Frequently Asked Questions Member Information Search Page
 SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC