DROP FUNCTION IF EXISTS GetNumberOfDays;
CREATE FUNCTION GetNumberOfDays(P_Date1 varchar(20), P_Date2 varchar(20))
RETURNS int(100)
BEGIN
DECLARE x INT;
DECLARE V_Date1 date;
DECLARE V_Date2 date;
SET V_Date1 := STR_TO_DATE(P_Date1, '%d/%m/%Y');
SET V_Date2 := STR_TO_DATE(P_Date2, '%d/%m/%Y');
SET x := 0;
WHILE V_Date1 <= V_Date2
DO
IF (DAYOFWEEK(V_Date1) != 7 AND DAYOFWEEK(V_Date1) != 1)
THEN
BEGIN
SET x = x + 1;
END;
END IF;
SET V_Date1 := DATE_ADD(V_Date1, INTERVAL 1 DAY);
END WHILE;
RETURN x;
END;
SELECT GetNumberOfDays('01/08/2018', '11/08/2018')
2条答案
按热度按时间dly7yett1#
试着用这个
声明@date1 datetime,@date2 datetime;
选择@date1='8/1/2018',@date2='8/7/2018'
选择datediff(dd,@date1,@date2)-(datediff(wk,@date1,@date2)*2)-case when datepart(dw,@date1)=1,然后选择1 else 0 end+case when datepart(dw,@date2)=1,然后选择1 else 0 end
kgsdhlau2#