• Hi,

    The following statement is untrue: "it is always a good practice to use DATEDIFF method for date comparison instead of making comparison using logical operators (,=)".

    As the previous two posters noted, using a function call on an indexed date column (or any indexed column, for that matter) prevents SQL Server from doing an efficient "seek" on the values in that column for that index - all the values need to be retrieved first, then the function applied. If the date column has the highest selectivity, then this can be a severe performance killer.

    A safer way to ensure that you get all dates in a certain range, ignoring the "time" component, without applying a function to the source column, is using DateAdd as follows:

    --select the correct data type here, DateTime vs SmallDateTime,

    -- as you might otherwise incurr another conversion function call

    -- on the source column!

    DECLARE @FromDate DateTime

    DECLARE @ToAndIncludingDate DateTime

    SET @FromDate = '2009-04-01'

    SET @ToAndIncludingDate = '2009-04-15'

    SELECT *

    FROM SomeTable

    WHERE (SomeColumn >= @FromDate AND SomeColumn < DateAdd(Day, 1, @ToAndIncludingDate) )

    HTH,

    Tao

    http://poorsql.com for T-SQL formatting: free as in speech, free as in beer, free to run in SSMS or on your version control server - free however you want it.