Sohail Arshed
This is an old post but it might help someone. This query returns the exact days, months and hours between two dates excluded All Sunday(s) and Monday(s):
```
declare @d1 datetime, @d2 datetime
select @d1 = '11/25/2019 12:00:00', @d2 = '12/02/2019 12:00:00'
SELECT (DATEDIFF(MINUTE, @d1, @d2) - DATEDIFF(WK, @d1, @d2) * 2880) + CASE WHEN DATEDIFF(WK, @d1, @d2) = 1 AND DATEDIFF(DW, @d1, @d2) <= 5 THEN 0 ELSE 1440 END
SELECT (DATEDIFF(HOUR, @d1, @d2) - DATEDIFF(WK, @d1, @d2) * 48) + CASE WHEN DATEDIFF(WK, @d1, @d2) = 1 AND DATEDIFF(DW, @d1, @d2) <= 5 THEN 0 ELSE 24 END
SELECT (DATEDIFF(DW, @d1, @d2) - DATEDIFF(WK, @d1, @d2) * 2) + CASE WHEN DATEDIFF(WK, @d1, @d2) = 1 AND DATEDIFF(DW, @d1, @d2) <= 5 THEN 0 ELSE 1 END
```
Or you can use this generic function:
```
CREATE FUNCTION [dbo].[fn_GetBusinnessDaysTimeSpan](
@DateFrom DATETIME,
@DateTO DATETIME,
@Type VARCHAR(50)
)
RETURNS BIGINT
AS
BEGIN
DECLARE @result AS BIGINT;
IF @Type = 'Min'
BEGIN
SET @result = (SELECT (DATEDIFF(MINUTE, @DateFrom, @DateTO) - DATEDIFF(WK, @DateFrom, @DateTO) * 2880) + CASE WHEN DATEDIFF(WK, @DateFrom, @DateTO) = 1 AND (DATEDIFF(DW, @DateFrom, @DateTO)) <= 5 THEN 0 ELSE 1440 END)
END
IF @Type = 'Hour'
BEGIN
SET @result = (SELECT (DATEDIFF(HOUR, @DateFrom, @DateTO) - DATEDIFF(WK, @DateFrom, @DateTO) * 48) + CASE WHEN DATEDIFF(WK, @DateFrom, @DateTO) = 1 AND (DATEDIFF(DW, @DateFrom, @DateTO)) <=5 THEN 0 ELSE 24 END)
END
IF @Type = 'Day'
BEGIN
SET @result = (SELECT (DATEDIFF(DW, @DateFrom, @DateTO) - DATEDIFF(WK, @DateFrom, @DateTO) * 2) + CASE WHEN DATEDIFF(WK, @DateFrom, @DateTO) = 1 AND (DATEDIFF(DW, @DateFrom, @DateTO)) <=5 THEN 0 ELSE 1 END)
END
RETURN @result
END
```