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但我正在寻找更好的解决方案。
发布于 2011-11-27 18:07:33
这是另一个解决方案:
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) < 6https://stackoverflow.com/questions/8284622
复制相似问题