Home Forums SQL Server 2005 T-SQL (SS2K5) Datediff versus Convert for datetime comparison (ignoring time part) RE: Datediff versus Convert for datetime comparison (ignoring time part)

  • To answer the question asked, the DateDiff will be more efficient than the convert. You might want to check out the "Common Date/Time Routines" link in my signature.

    However, neither of these will be very efficient in a where clause... neither is SARGable, and neither will be able to utilize an index on the column. The best you can hope for with these is an index scan, though you may also get a table scan or clustered index scan. If this is a large table, just take a lunch break while it's running.

    Elliott's suggestion to have a persisted, calculated column would work out here - just add an index to that column, and you'll be able to take care of both problems.

    Wayne
    Microsoft Certified Master: SQL Server 2008
    Author - SQL Server T-SQL Recipes


    If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
    Links:
    For better assistance in answering your questions
    Performance Problems
    Common date/time routines
    Understanding and Using APPLY Part 1 & Part 2