• Another set based solution, this time using the good old Tally table[/url]. ((c) Jeff Moden et al. 19xx-2009)

    SELECT TOP 1

    DATEADD(dd,N-1,GETDATE())

    FROM

    Tally

    WHERE

    n < 366 --== One years worth of days

    AND

    DAY(DATEADD(dd,N-1,GETDATE())) = 13

    AND

    --==

    --== Using @@DATEFIRST and the modulus operator we don't need to change or

    --== assume anything about the current DATEFIRST setting

    --==

    (@@DATEFIRST + DATEPART(dw,DATEADD(dd,N-1,GETDATE()))) %7 = 6

    ORDER BY

    N

    The execution plan for this indicates 100% of the query is spent doing a clustered index seek (nice!). That's if you build your tally table with a clustered index of course, as demonstrated in Jeff's tally table article.

    And, as I indicated in the comments, you don't need to mess about with the DATEFIRST setting.