This assumes that neither from_time nor to_time is on weekend
select name,
CASE
WHEN DATEDIFF(day, from_time, to_time) = 0 THEN DATEDIFF(mi, from_time, to_time)
ELSE 540 * (select count(*) - 2 from fnSeqDates(from_time, to_time) where seqdate between 2 and 6) + -- Number of minutes for whole day, not including from_time and to_time
1080 - datediff(mi, '09:00:00', from_time) % 1440 + -- Numbers of minutes from from_time to closing at 6 pm same day.
datediff(mi, '09:00:00', to_time) % 1440 -- Number of minutes since 9 am to to_time same day
END Mins
FROM MyTable
with the help of my function found here
http://www.sqlservercentral.com/colu...tiliretire.asp