• Using a Tally Table would improve this code significantly, also you have to be careful about using the fucntion datepart(dw,a_date) as in some cases Sunday isnt always represented by 1, and will depend on your regional settings.

    Eg : In the US, DW 1 is Sunday, yet in europe it can be DW 1 is monday.

    See books on line http://msdn.microsoft.com/en-us/library/ms174420.aspx

    To mitigate this you need to use the SET DATEFIRST to make it consistent across all regional settings, see http://msdn.microsoft.com/en-us/library/ms181598.aspx

    _________________________________________________________________________
    SSC Guide to Posting and Best Practices