• Thanks, Mark,

    DATEDIFF is the tough one for most folks because they frequently don't understand that it works on the BOUNDARIES of the datepart. For example, a DATEDIFF(yy,'2014-12-31 23:59:59.997','2015-01-01 00:00:00.000') will return a "1" even though the two dates are only 3ms apart.

    As for CROSS APPLY, it's the same as a correlated subquery in the SELECT list except that it can return more than one value for each row without blowing up. Paul White wrote really good articles on the subject. Here are the links to those articles...

    http://www.sqlservercentral.com/articles/APPLY/69953/

    http://www.sqlservercentral.com/articles/APPLY/69954/

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)