Pankajmittal’s Blog

September 15, 2011

calculate sunday between two dates

–Pass two dates and it how many sundays occures
CREATE FUNCTION [dbo].[fnc_SundayCount](@StartDate datetime,@EndDate datetime)
RETURNS SMALLINT
as
Begin
SET @StartDate=CONVERT(datetime,CONVERT(VARCHAR, @StartDate,101))
SET @EndDate=CONVERT(datetime,CONVERT(VARCHAR, @EndDate,101))
DECLARE @DayDiff SMALLINT
DECLARE @tempVar SMALLINT
DECLARE @TotalSunday SMALLINT
SET @TotalSunday=0
SET @DayDiff=DATEDIFF(DAY,@StartDate,@EndDate)
SET @tempVar=0
WHILE (@tempVar < @DayDiff)
BEGIN
IF(DATEPART(weekday,@StartDate)=1)
BEGIN
SET @TotalSunday =@TotalSunday+1
END
SET @StartDate=@StartDate+1
SET @tempvar = @tempvar+1
END
Return @TotalSunday
END

Advertisement

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Theme: Rubric. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.