how to find no of sunday after specific date in a month in s
how to find no of sunday after specific date in a month in sql
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Manish Kumar ChoudharyPosted Mar 18, 2015, 5:44 AM
Create Function NumberOfSundays(@dFrom datetime, @dTo datetime)
Returns int as
BEGIN
Declare @nSundays int
Set @nSundays = 0
While @dFrom <= @dTo Begin
If datepart(dw, @dFrom) = 1 Set @nSundays = @nSundays + 1
Set @dFrom = DateAdd(d, 1, @dFrom)
End
Return (@nSundays)
END
GO
-- Number of Sundays
Print dbo.NumberOfSundays()
-- Number of Other days
Print DateDiff(d, '2009/06/01', '2009/06/30') + 1 - dbo.NumberOfSundays('2009/06/01', '2009/06/30')