Select * from tbl where ts between convert(datetime,convert(varchar,ts, 101)) + convert(datetime,'9:30 am') And Convert(datetime,convert(varchar,ts,101)) + convert(datetime,'5:30 pm')
This, I believe, will use the index. I can not verify, I am sending this using ipod
[EDIT from my computer]
create function dbo.DaysTime(@date as datetime, @time datetime) returns datetime as begin return dateadd(d, datediff(d, 0, @d), 0) + @time end select * from tbl where ts between dbo.DaysTime(ts, '9:30 AM') and dbo.DaysTime(ts, '5:30 PM')
This will still use the index, since we do not put the expression on the left side of the where clause.
source share