Given a date, return nth weekday of month?

  • Hi there -

    I can't seem to gather the steam to figure this out today.

    I need to find out what the nth weekday of the month is when given a date.

    Ex.

    today is 7/17/2012

    I want to see that it is the 3rd Tuesday of this month (3 Tuesday - something to that effect).

    I must be making this out to be harder than it is.

    Thanks for any input you have,

    matt

  • Start with this:

    SELECT

    ceiling(DAY(GETDATE()) / 7.),

    DATENAME(dw, GETDATE()),

    ceiling(DAY('20120731') / 7.),

    DATENAME(dw, '20120731');

  • Thank you! Perfect, I knew it had to be something simple.:blush:

  • Lynn Pettis (7/17/2012)


    Start with this:

    SELECT

    ceiling(DAY(GETDATE()) / 7.),

    DATENAME(dw, GETDATE()),

    ceiling(DAY('20120731') / 7.),

    DATENAME(dw, '20120731');

    I LOVE SIMPLE!!! 😀

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

  • I have written an article solving exactly this problem with a nice UDF. It is here: Nth Weekday of the Month

    [/url]

  • Works like a charm, thanks! 😀

Viewing 6 posts - 1 through 5 (of 5 total)

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