查看: 449|回复: 0

[SQLServer] 通过月份得到本月有几天周末

发表于 2018-4-27 08:00:05

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



回复

使用道具 举报