Question: I have a table in SQL Server 2005 which has three columns: 

```
id (int), 
message (text), 
timestamp (datetime)

```

There is an index on timestamp and id.

I am interested in doing a query which retrieves all messages for a given date, say '12/20/2008'. However I know that simply doing where timestamp='12/20/2008' won't give me the correct result because the field is a datetime field. 

Somebody had recommended using the DATEPART function and pulling the year, month, and day out of timestamp and verifying that these are equal to 2008, 12, and 20, respectively. It looks like this would not use the index I have on timestamp and would end up doing a full table scan.

So what is the best way to construct my query so that I am taking advantage of the index that I have created?

Answer: 
The use of two datetime variables has always worked infallibly in my experience. The issue of the resolution seems highly unlikely. The important fact to remember, however, is that a range (of any type) includes both end points. So you can't test using BETWEEN on two dates, because it will include both. Rather use something like 

datefield >= @startdate AND datefield < @enddate

[The Manual.](http://msdn.microsoft.com/en-us/library/aa225976.aspx)

C'mon folks - the documentation for this isn't *that* hard to find. :D
The BETWEEN statement can help you. 

```
SELECT *
FROM MyTable
WHERE TimeStamp BETWEEN @Start AND @End;

```

Start would need to be something like 12:01 am for the day you want messages for, and end would be like 11:59pm for the end of the same day.