DateTime Efficiency and Best Practices Question

I use this convert, float, and floor things a lot, and I'll admit that sql is probably not my strongest skill.  It seems like I remember starting doing this when I got invalid results, but someone told me that it was very inefficient, and I do a lot of date/time based queries in this app, and I really need to get it right.  

Here are some examples:
-- Gets it based on certain dates... say all orders from January 10th to 13th
Select avg(datediff(second,w.dispBeginTime,w.dispFinishTime))
  From WorkOrder w
  Where      w.keyCompany = @keyCompany
      and      w.dispBeginTime  is not null
      and w.dispFinishTime is not null
      and floor(convert(float,w.dispFinishTime)) >= floor(convert(float,@BeginDate))
      and floor(convert(float,w.dispFinishTime)) <= floor(convert(float,@EndDate))

-- Gets it based on certain times... say all orders from January 10th 8:01:01 to January 13th 8:02:02
Select avg(datediff(second,w.dispBeginTime,w.dispFinishTime))
  From WorkOrder w
  Where      w.keyCompany = @keyCompany
      and      w.dispBeginTime  is not null
      and w.dispFinishTime is not null
      and convert(float,w.dispFinishTime) >= convert(float,@BeginDate)
      and convert(float,w.dispFinishTime) <= convert(float,@EndDate)

Thanks, Tony
LVL 1
picsnetAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Anthony PerkinsCommented:
Try it this way:

Select @BeginDate = CAST(CONVERT(char(8), @BeginDate, 112) as datetime),      -- Or smalldatetime
          @EndDate = CAST(CONVERT(char(8), @EndDate, 112) as datetime)

Select ...
Where ...

 and CONVERT(char(8), w.dispFinishTime, 112) BETWEEN @BeginDate And @EndDate


It is still not going to be able to take advantage of any index, but it should be better.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
brejkCommented:
@acperkins: Have you made any tests? I did some test yesterday (I tried three methods of cutting off the time: with CONVERT, with FLOOR, and  with DATEADD functions). And it seems to me like their performance is very similar (or even the same). But if you have any test results available or maybe you can provide some link to some reliable results of someone else I would be grateful.
0
picsnetAuthor Commented:
i did something similar where i used the between, but i kept my dates in floats....
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.