try this, you might have to put it in a function
DECLARE @Date DATETIME,
@StartOfDay FLOAT,
@EndOfDay FLOAT,
@DateAdd DATETIME
SELECT @Date ='2009-06-26 15:45:00.000',
@StartOfDay = 8.5,
@EndOfDay = 17.5,
@DateAdd = '1900-01-01 09:00:00.000'
--fix up start date
--before start of day, move to start of day
IF ((CAST(@Date - DATEADD(dd,0, DATEDIFF(dd,0,@Date)) AS FLOAT) * 24) < @StartOfDay)
BEGIN
SET @Date = DATEADD(mi, @StartOfDay * 60, DATEDIFF(dd,0,@Date))
END
--after close of day, move to start of next day
IF ((CAST(@Date - DATEADD(dd,0, DATEDIFF(dd,0,@Date)) AS FLOAT) * 24) > @EndOfDay)
BEGIN
SET @Date = DATEADD(mi, @StartOfDay * 60, DATEDIFF(dd,0,@Date)) + 1
END
--move to monday if on weekend
WHILE DATENAME(dw, @Date) IN ('Saturday','Sunday')
BEGIN
SET @Date = @Date + 1
END
--get the number of hours to add and the total hours per day
DECLARE @HoursPerDay FLOAT
DECLARE @HoursAdd FLOAT
SET @HoursAdd = DATEDIFF(hh, '1900-01-01 00:00:00.000', @DateAdd)
SET @HoursPerDay = @EndOfDay - @StartOfDay
--date the time of geiven day
DECLARE @CurrentHours FLOAT
SET @CurrentHours = CAST(@Date - DATEADD(dd,0, DATEDIFF(dd,0,@Date)) AS FLOAT) * 24
--if we stay in the same day, all is fine
IF (@CurrentHours + @HoursAdd <= @EndOfDay)
BEGIN
SET @Date = @Date + @DateAdd
END
ELSE
BEGIN
--remove part of day
SET @HoursAdd = @HoursAdd - (@EndOfDay - @CurrentHours)
--,ove to next day
SET @Date = DATEADD(dd,0, DATEDIFF(dd,0,@Date)) + 1
--loop day
WHILE @HoursAdd > 0
BEGIN
--add day but keep hours to add same
IF (DATENAME(dw,@Date) IN ('Saturday','Sunday'))
BEGIN
SET @Date = @Date + 1
END
ELSE
BEGIN
--add a day, and reduce hours to add
IF (@HoursAdd > @HoursPerDay)
BEGIN
SET @Date = @Date + 1
SET @HoursAdd = @HoursAdd - @HoursPerDay
END
ELSE
BEGIN
--add the remainder of the day
SET @Date = DATEADD(mi, (@HoursAdd + @StartOfDay) * 60, DATEDIFF(dd,0,@Date))
SET @HoursAdd = 0
END
END
END
END
SELECT @Date
Hope that helps