vitex2003 (1/30/2014)
SELECT RangeType,StartDate,EndDate,CommentFROM dbo.TestTable
WHERE convert(char(10),EndDate,102) >= cast('01 Oct 2013' as datetime)
AND convert(char(10),StartDate,102) <= cast('01 Nov 2013' as datetime)
Are you sure about that? You are now evaluating a string on one side to a date on the other. Do you know what really happens there? It will promote your string to a datetime. In other words you are explicitly casting a datetime to a string and then the sql engine will implicitly convert it right back to a datetime. This is good because if the engine compared them as strings your logic would not work correctly.
Take a look at the values from your convert.
SELECT RangeType,StartDate,EndDate,Comment
,convert(char(10),EndDate,102) as ConvertEndDate, cast('01 Oct 2013' as datetime)
, convert(char(10),StartDate,102) as ConvertStartDate, cast('01 Nov 2013' as datetime)
FROM dbo.TestTable
WHERE convert(char(10),EndDate,102) >= cast('01 Oct 2013' as datetime)
AND convert(char(10),StartDate,102) <= cast('01 Nov 2013' as datetime)
And adding the functions around EndDate and StartDate in your where clause has rendered this query nonsargable to boot.
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/