• You will have to use DERIVED TABLE to do this

    SELECTTOP 10 *

    FROM(

    SELECT key_value

    ,convert(datetime2,PV.key_value) as DT2

    ,convert(datetime,CONVERT(DATETIME2, PV.key_value)) AS DT

    FROM parameter_value PV

    WHERE PV.datatype = 'D'

    ) AS PV

    WHEREconvert(datetime,CONVERT(DATETIME2, PV.key_value)) BETWEEN GETDATE() and DATEADD(d, 10, GETDATE())

    This issue occurs because the query optimizer is evaluating the WHERE CLAUSE involving key_value first and then the one for datatype

    Hence, its better to force it this way using a DERIVED TABLE.


    Kingston Dhasian

    How to post data/code on a forum to get the best help - Jeff Moden
    http://www.sqlservercentral.com/articles/Best+Practices/61537/