Pages

Monday, April 18, 2011

Finding Sunday In Month


Create PROCEDURE GetSundaysbyyearandmonth --2010,8
--2010 is year and 8 is month
@year varchar(10),
@month varchar(10)
as
begin
declare @date varchar(20)
--@first day is used to find the first day name in a month
declare @firstday varchar(50)
declare @start int
set @date=@month+'/'+'01'+'/'+@year
--print @date
declare @totaldays varchar(50)
set @totaldays=day(dateadd(mm,datediff(mm,-1,@year),-1))
set @firstday = datename(dw,@date)
--print @firstday
--SELECT datepart(dd,(getdate()));

set @start=
case @firstday when 'Monday'then 7
when 'Tuesday' then 6
when 'Wednesday'then 5
when 'Thursday'then 4
when 'Friday'then 3
when 'Saturday'then 2
else 1
end
print('sunday in given month are')
while(@start<=@totaldays)
begin
print @start
set @start=@start+7
end
end
************************************************

No comments:

Post a Comment