0

I have to find the number of business days using mysql.I am using this query but this is not giving me the correct result

 SELECT ((DATEDIFF('2015-05-31', '2015-05-01')) -((WEEK('2015-05-31') - WEEK('2015-05-01')) * 2) - 
(case when weekday('2015-05-31') = 6 then 1 else 0 end) - (case when weekday('2015-05-01') = 5 then 1 else 0 end))
as DifD ;

It is giving 19 as output where number of business days should be 20 Somebody please help

lucifer
  • 2,297
  • 18
  • 58
  • 100

2 Answers2

1

Try this!

SET @i=-1;
SELECT SUM(CASE WHEN(WEEKDAY(ADDDATE('2015-05-01', INTERVAL @i:=@i+1 DAY))) < 5 THEN 1 ELSE 0 END) AS `business_days`
FROM `table`
WHERE @i < DATEDIFF('2015-05-31', '2015-05-01');

Hope this answer helps!

George K.
  • 41
  • 2
0
drop procedure COUNTWEEKDAYS;
DELIMITER $$

CREATE PROCEDURE COUNTWEEKDAYS (FROMDATE TIMESTAMP, TODATE TIMESTAMP)
begin
declare NOOFWEEKDAYS INTEGER;
set NoOfWeekDays = (datediff(todate, fromdate) + 1)
            -((timestampdiff(week, FROMDATE , TODATE) * 2))
            -weekday(fromdate)%4
            -weekday(todate)%4;
select NOOFWEEKDAYS;
end$$