欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-03-27 15:28:01
CREATE FUNCTION getweekdayCount ( @month varchar(10)) RETURNS INT AS BEGIN--declare @month varchar(10)--set @month='201802' 通过月份得到本月有几天周末DECLARE @DAYC ......

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