// Create function
Create FUNCTION
[dbo].[Get_NameOf_Days](
@startdate
datetime
,@enddate datetime
,@DayValue int
-- return the value of days (starting day with
sunday- value=1)
,@NameValue int
-- return the name of days (starting day with monday-
value=0)
)
RETURNS VARCHAR(MAX)
BEGIN
declare @count int
=0
while
@startdate<=@enddate
Begin
IF DatePart(WEEKDAY,@startdate) = @DayValue
SET
@count=@count+1
SET
@startdate=DateAdd(d,1,@startdate)
END
RETURN (select DATENAME(dw,@NameValue)+' : '+CAST(@count as varchar))
END
// Call function
select dbo.Get_NameOf_Days('2017-1-1','2017-1-31',1,6)
// Result
Very helpful function sir, Thank you
ReplyDelete