• Nigel,

    I think you missed my post on page 1. It has as a similar implementation, and it allows to find the first sunday - saturday of the month. I did not spend time modifying the calculation of the first of the month. Your implementation is nice though.

    So here is the modified code applying the new first day of month logic

    declare @DW int

    set @DW = 7 -- [Sunday - Saturday] == [1 - 7]

    declare @d datetime

    set @d = '7/21/2009'

    declare @first datetime

    set @first = DATEADD(d,1-DAY(@d),@d)

    declare @dayofweek int

    set @dayofweek = DATEPART(dw,@first)

    print @first + (7+(@dw - @dayofweek))%7