• Lynn Pettis (6/2/2016)


    The following should help:

    d e c la r e -- spaced to post from work

    @ThisDate date = getdate();

    select

    @ThisDate,

    dateadd(quarter,datediff(quarter,0,@ThisDate),0) CurrentQuarterStart,

    dateadd(quarter,datediff(quarter,0,@ThisDate) - 1,0) PreviousQuarterStart,

    dateadd(quarter,datediff(quarter,0,@ThisDate),-1) PreviousQuarterEnd;

    Personally, I would use >= PreviousQuarterStart and < CurrentQuarterStart for a filter in a WHERE clause.

    +1 for the simplicity and + 1,000,000 for the WHERE clause example to avoid accidental inclusion of whole dates for the next quarter.

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