• cmick 77911 (4/29/2016)


    I would turn it into a column and order by it. Cleaner this way.

    SELECT

    (CASE WHEN AddDt > UpdateDt

    THEN AddDt

    ELSE ISNULL(UpdateDt, AddDt)

    END) AS MaxDt,

    Id, Name, OnHand, AddDt, UpdateDt

    FROM @widgits

    ORDER BY 1

    Using ordinal position is a bad practice. Anytime, anyone can change the column order and the results would change without notification. To prevent this, use column alias.

    SELECT

    (CASE WHEN AddDt > UpdateDt

    THEN AddDt

    ELSE ISNULL(UpdateDt, AddDt)

    END) AS MaxDt,

    Id, Name, OnHand, AddDt, UpdateDt

    FROM @widgits

    ORDER BY MaxDt

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2