• For too many reasons to list in this short amount of space, I strongly recommend that you never process dates by individual Year and/or Month components. Some of the others have already suggested how to handle things... always treat dates as a range... yep... even if the date is for one measely day. WHERE clauses should always follow the general format of...

    WHERE somedatecol >= @StartDate

    AND somedatecol < @EndDate+1

    That is assuming, of course, that you are working with whole dates that either have no literally expressed time component (defaults to midnight) or has a time component of precisely 00:00:00.000. Other considerations will need be made if @StartDate or @EndDate have a non-midnight time component. Doesn't matter if "somedatecol" does or not and that's the beauty of the method shown above. And, it'll allow for very high performance Index SEEKs if the correct indexes are available.

    You will also find those that suggest that you use one of the following...

    WHERE somedatecol BETWEEN @StartDate AND DATEADD(ms,-3,@EndDate+1)

    WHERE somedatecol BETWEEN @StartDate AND @EndDate+'23:59:59.997

    Treat them just like street drugs... just say "NO". 😉 Do they work? Yes, today they do... when 2008 comes out, it will "depend". 😉

    --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)