Return to Snippet

Revision: 49017
at July 14, 2011 01:12 by aamirrajpoot


Initial Code
DELIMITER $$

DROP FUNCTION IF EXISTS `numWeekends`$$

CREATE FUNCTION `numWeekends`(first_date DATE, second_date DATE, str_weekends VARCHAR(50)) RETURNS INT(11)
    DETERMINISTIC
BEGIN
  DECLARE start_date DATE;
  DECLARE end_date DATE;
  DECLARE diff INT DEFAULT 0;
  DECLARE dayofweekint INT;
  IF (first_date < second_date) THEN
    SET start_date = first_date;
    SET end_date = second_date;
  ELSE
    SET start_date = second_date;
    SET end_date = first_date;
  END IF;
  
  WHILE start_date <= end_date DO
  SET dayofweekint = DAYOFWEEK(start_date) - 1;
  IF
   (dayofweekint < 1)
  THEN
   SET dayofweekint = 7;
  END IF;
  IF
   (CONCAT('[[:<:]]', str_weekends, '[[:<:]]') REGEXP dayofweekint)
  THEN
   SET diff = diff + 1;
  END IF;
 SET start_date = DATE_ADD(start_date,INTERVAL 1 DAY);
  END WHILE;
  
  RETURN diff;
    
END$$

DELIMITER ;

Initial URL

                                

Initial Description
A function to get off days inside mySQL

Initial Title
Find off days in a date range using mysql

Initial Tags

                                

Initial Language
MySQL