Date of the 3rd Thursday of each quater (Mar, Jun, sep,dec)

  • I need to work out the date of the 3rd Thursday of each quater (Mar, Jun, sep,dec) and to which of these today's date is the closest but not the Thursday in the past.

    e.g. input = todays date

    output = 17th Dec 2009 (this is the next 3rd thursday of the current quater)

    Thanks

  • Use a calendar table...

    http://sqlserver2000.databases.aspfaq.com/why-should-i-consider-using-an-auxiliary-calendar-table.html%5B/url%5D

    Presumably you know the first date of the quarter so finding the third thursday would be straight

    forward.



    Clear Sky SQL
    My Blog[/url]

  • Calendar table is definitely the best way to go.

    Here's a mathematical method, using a Numbers table.

    ;

    WITH CTE(Date, Seq)

    AS (SELECT

    DATEADD(day, number, '1/1/2009'),

    row_number() OVER (PARTITION BY DATEPART(year,

    DATEADD(day, number, '1/1/2009')),

    DATEPART(month,

    DATEADD(day, number, '1/1/2009')) ORDER BY number)

    FROM

    dbo.Numbers

    WHERE

    DATEPART(weekday, DATEADD(day, number, '1/1/2009')) = 5

    AND DATEPART(month, DATEADD(day, number, '1/1/2009')) IN (3, 6, 9, 12))

    SELECT

    MIN(Date) AS Date

    FROM

    CTE

    WHERE

    Seq = 3

    AND Date > GETDATE() ;

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • many thanks, this worked perfectly. 🙂

Viewing 4 posts - 1 through 3 (of 3 total)

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