I have created this script but looking for more optimization regarding performance--
DECLARE @count int
SET @count = 30
DECLARE @sundays int
SET @sundays = 0
DECLARE @startdate datetime
SET @startdate ='2012-01-01'
DECLARE @enddate datetime
SET @enddate = DATEADD(DD,@count,@startdate)
PRINT @enddate
PRINT 'Before Sunday'
WHILE @startdate <= @enddate
BEGIN
IF DATEPART(DW,@startdate) = 1
BEGIN
SET @sundays = @sundays + 1
SET @enddate = DATEADD(DD,1,@enddate)
PRINT @enddate
PRINT 'After Sunday'
PRINT @sundays
END
SET @startdate = DATEADD(DD,1,@startdate)
END
_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/