Querying a smalldatetime's date and time seperately in SQL server?

Posted by Kylee on Stack Overflow See other posts from Stack Overflow or by Kylee
Published on 2010-03-19T17:12:22Z Indexed on 2010/03/19 17:21 UTC
Read the original article Hit count: 119

Filed under:

Imagine a table that has two fields, a smalltimedate and an int and about 1000 rows of data. What I'm attempting to do in query is to find the average of the INT field for rows between 3/3/2010 - 3/13/2010 and only if the entry is between 6:00am - 11:00pm.

I tried

between '2010-03-03 06:00 AND 2010-03-13 23:00'

However that only restricts that very beginning and end times. I could do this with a loop but I'm going to need to have the same query run over much larger date ranges and this will quickly eat server resources. Is there a way to query date and time seperately?

© Stack Overflow or respective owner

Related posts about sql-server