DATEDIFF功能可以找出两个日期之间的日期差异。我只是想知道是否有任何简单的方法可以找到不包括周末的日期差异?
我创建了下一个代码:
SET @start = CONVERT(datetime, '23.11.2011', 104)
SET @finish = CONVERT(datetime, '29.11.2011', 104)
SET @result = 0
WHILE @start < @finish
BEGIN
IF (DATEPART(dw, @start) <> 7) AND (DATEPART(dw, @start) <> 1)
BEGIN
SET @result = @result + 1
END
SET @start = DATEADD(dd, 1, @start)
END
PRINT @result
但我正在寻找更好的解决方案。
答案 0 :(得分:0)
这是另一个解决方案:
set datefirst 1
;with cte as
(select cast('20111101' as datetime) as dt union all select dt+1 from cte where dt<'20111201')
select count(*) as cnt from cte
where dt between '20111123' and '20111129'
and datepart(dw, dt) < 6