通過月份得到本月有幾天週末

心韻發表於2018-04-16

CREATE FUNCTION getweekdayCount ( @month varchar(10))
RETURNS INT
AS
BEGIN
–declare @month varchar(10)
–set @month=`201802`
—通過月份得到本月有幾天週末
DECLARE @DAYCOUNT int
DECLARE @datestart datetime
DECLARE @dateend datetime

set @datestart =cast(SUBSTRING( @month,0,5)+`-`+SUBSTRING( @month,5,2)+`-01` as datetime)
set @dateend=dateadd(ms,-3,DATEADD(mm, DATEDIFF(m,0,@datestart)+1, 0))
SET @DAYCOUNT=0
while @datestart<=@dateend
begin

if(datename(weekday,@datestart)= `星期日` or datename(weekday,@datestart)=`星期六`) begin
set @DAYCOUNT=@DAYCOUNT+1
end
SET @DATESTART=DATEADD(DAY,1,@DATESTART)

end

RETURN(@DAYCOUNT)
END

相關文章