Monday, March 26, 2012

Retriew number of sunday,s for a given month in sql server

hello i am student and developing project having the fallowing problem

1: i want to calculate the number of sunday,s in a given month.

OR between two given dates like 01/02/06 and 28/02/06 .

thanks

Hi,

this should solve the problem, input is the day of the week you want to get.

CREATE FUNCTION NumberOfWeekdays
(
@.StartDate DATETIME,
@.EndDate DATETIME,
@.Weekday TINYINT
)
RETURNS INT
BEGIN
DECLARE @.dt DATETIME
DECLARE @.NumberofDays INT
SET @.NumberofDays = 0

SET @.dt = @.StartDate
WHILE @.dt < @.EndDate
BEGIN
SET @.NumberofDays = @.NumberofDays + (CASE WHEN DATEPART(dw,@.dt) = @.Weekday THEN 1 ELSE 0 END)
SET @.dt = @.dt+1
END
RETURN @.NumberofDays
END

HTH; Jens Suessmeyer.

No comments:

Post a Comment