日期:2014-05-17 浏览次数:20519 次
--指定月份天数 declare @m varchar(7) set @m='2012-08' select datediff(day,@m+'-01',dateadd(month,1,@m+'-01')) --指定月份工作天数 declare @dt datetime,@dt2 datetime,@i int set @dt=convert(datetime,@m+'-01') set @dt2=dateadd(month,1,@dt) while @dt<@dt2 begin if datepart(weekday,@dt)<>1 and datepart(weekday,@dt)<>6 set @i=isnull(@i,0)+1 set @dt=dateadd(day,1,@dt) end select @i
------解决方案--------------------
create function getWorkDays( @year int , @month int ) returns int as begin declare @CountDay int declare @temptime datetime set @CountDay = 0 set @temptime = convert(varchar,@year) + '-'+ convert(varchar,@month) + '-01' while(MONTH(@temptime) = @month) begin if DATEPART(weekday,@temptime) <>1 and DATEPART(weekday,@temptime)<>7 begin set @CountDay = @CountDay + 1 End set @temptime = DATEADD(day,1,@temptime) end return @CountDay end --调用 select dbo.getWorkDays(2009,7)