views:

23

answers:

1

I just want to check my logic there.

Suppose I want to find all new products in the last 30 days. My current procedure is this:

SELECT ProductName
FROM ProductTable
WHERE DATEDIFF( d, CreateDate, GETDATE() ) < 30

However, I understand that functions like DATEDIFF will not use the non-clustered index I have created on CreateDate. So therefore, will my query run quicker using the following procedure:

SELECT ProductName
FROM ProductTable
WHERE CreateDate >= DATEADD( d, -30, GETDATE() ) AND CreateDate < GETDATE()

BTW, I don't have SQL Server from where I am so I cant test this using the execution plan.

+1  A: 

Yup, you are correct, the second query will be faster as it can use any indexes on CreateDate that are available.

Binary Worrier
cool thanks for that!
Nai