1

The DATEDIFF function allows to find out the dates difference between the two dates. I am just wondering if there is any easy way to find the dates difference excluding weekends?

I have created next code:

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

But I am looking for the better solution.

ceth
  • 44,198
  • 62
  • 180
  • 289
  • 1
    possible duplicate of [Count work days between two dates in T-SQL](http://stackoverflow.com/questions/252519/count-work-days-between-two-dates-in-t-sql) – Rubens Farias Nov 27 '11 at 09:46

1 Answers1

0

Here is another one solution:

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
ceth
  • 44,198
  • 62
  • 180
  • 289