Returning data from table based on date ranges

  • Jeff Moden wrote:

    @Jeffrey Williams...

      <li style="list-style-type: none;">

    1. Due to your hard-coded offsets for the start of each quarter, what's going to happen on a Leap Year?

    Nothing - the hard-coded offsets are actually the integer value for the dates in the year 1900.  They are not the number of days from the beginning of a year - so taking the difference in years between 0 (1900-01-01) and @fromDate will result in adding xx years to 1900-03-25 (83).

      <li style="list-style-type: none;">

    1. Although probably not likely but still entirely possible, what's going to happen if the difference between @FromDate and @ToDate exceeds 100 months?

    You are correct - if the date range exceeds 8+ years then this function will not work correctly.  It is easy enough to modify by adding another t table to the cross join to increase the range if needed.

    Good comment on the start date issue for the quarter.  I was going to hum a rock at the OP on that one but then saw your good comment.

    Why not sling a pork-chop?

    Jeffrey Williams
    “We are all faced with a series of great opportunities brilliantly disguised as impossible situations.”

    ― Charles R. Swindoll

    How to post questions to get better answers faster
    Managing Transaction Logs

  • Ah... understood on the offsets.  Thanks, Jeff.

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

  • Hi @Jeffrey Williams,

    Wow, thank you for taking the time to put that together. I have tried it on my machine and it looks good. The production server this will run on is actually SQL 2008R2 so I think it will need tweaking to run on that as some of the functions are not compatible.

    Thanks again to all who have looked and commented.

  • You can remove the CONCAT function or modify to use + to build that string.  That column isn't used.

    Jeffrey Williams
    “We are all faced with a series of great opportunities brilliantly disguised as impossible situations.”

    ― Charles R. Swindoll

    How to post questions to get better answers faster
    Managing Transaction Logs

Viewing 4 posts - 16 through 18 (of 18 total)

You must be logged in to reply to this topic. Login to reply