日期:2014-05-18 浏览次数:20573 次
declare @dateBegin datetime, @dateEnd datetime, @dateTmp datetime declare @day int, @i int, @j int select @dateBegin = '20071001', @dateEnd = getDate() select @day = datediff(dd,@dateBegin,@dateEnd) select @i = 1, @j = 1 while (@i <= @day) begin select @dateTmp = dateadd(dd,@i,@dateBegin) if (datePart(weekday,@dateTmp) > 1 and datePart(weekday,@dateTmp) < 7) select @j = @j + 1 select @i = @i + 1 end select @j '两个日期差距的天数'
------解决方案--------------------
按 hb_gx 的方法 写成一个函数
------解决方案--------------------
算法可以这样
1. 先求出差几天
2. 再除以 7 算出是 N = 几周, -> 2N
3. 再判断 被7整除的余数有几天是周六,周日, m
4. select 2N + m
---------
这样快一点
------解决方案--------------------
declare @k table(datestr datetime) insert into @k select '2007-10-01' union all select '2007-10-02' union all select '2007-10-03' union all select '2007-10-04' union all select '2007-10-05' union all select '2007-10-06' union all select '2007-10-07' union all select '2007-10-08' union all select '2007-10-09' union all select '2007-10-10' union all select '2007-10-11' union all select '2007-10-12' union all select '2007-10-13' union all select '2007-10-14' select * from @k select count(1) from @k where convert(varchar(10),datestr,120) between '2007-10-01' and '2007-10-14' and datepart(dw,datestr)<>7