sql 计算某个时间段周六,日总数
Create PROCEDURE Busdays @from_dt datetime,@to_dt datetime ,@wkends int output
AS
DECLARE
@days int,--天数
@dp int ,--星期?
@cnt int --计数
set @wkends=0 --非工作日天数
set @cnt=0
set @days=datediff(d,@from_dt,@to_dt)
while @cnt <=@days
begin
set @dp=datepart(dw,dateadd(day,@cnt,@from_dt))
if @dp=1 or @dp=7
set @wkends=@wkends+1
set @cnt=@cnt+1
End
--------------------
--------------------
--调用
declare @ret int
exec Busdays '2005-2-1','2005-2-8', @ret output
print '总共'+cast(@ret as nvarchar(10))+'个周六,日'