程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> MYSQL數據庫 >> MySQL綜合教程 >> SQL實現工作日計算

SQL實現工作日計算

編輯:MySQL綜合教程

 

CREATE FUNCTION Time_Span_of_minutes(  

start_day DATETIME,  

end_day DATETIME)  

RETURNS FLOAT 

BEGIN 

            -- 返回按分鐘計算兩段時間的間隔,采用逐日靠近的方法求解,一天按8小時480分鐘計算,周末不計,不考慮法定節假日。  

            -- 如果起始日期在周末,則計算當天時間。  

            DECLARE minutes FLOAT;   

            DECLARE next_day DATETIME;  

            SET minutes=0.0;  

            SET next_day=start_day;  

                  

            IF(TIMESTAMPDIFF(DAY,start_day,end_day)<1.0) THEN 

                    SET minutes=minutes+TIMESTAMPDIFF(MINUTE,start_day,end_day);  

            ELSE 

                    WHILE TIMESTAMPDIFF(DAY,next_day,end_day)>=1.0 DO  

                            IF ((DAYOFWEEK(next_day)=7) OR (DAYOFWEEK(next_day)=1)) THEN 

                                    SET next_day=next_day+INTERVAL 1 DAY;  

                            ELSE 

                                    SET next_day=next_day+INTERVAL 1 DAY;  

                                    SET minutes=minutes+480.0;  

                            END IF;  

                    END WHILE;  

                    SET minutes=minutes+TIMESTAMPDIFF(MINUTE,next_day,end_day);  

                    IF ((DAYOFWEEK(start_day)=7) OR (DAYOFWEEK(start_day)=1)) THEN 

                            SET minutes=minutes+TIMESTAMPDIFF(MINUTE,start_day,CONVERT(CONCAT(SUBSTRING(start_day FROM 1 FOR 10),' 17:30:00'),DATETIME));  

                    END IF;  

            END IF;  

            RETURN minutes;  

END; 

 

CREATE FUNCTION Time_Span_of_minutes(

start_day DATETIME,

end_day DATETIME)

RETURNS FLOAT

BEGIN

                     -- 返回按分鐘計算兩段時間的間隔,采用逐日靠近的方法求解,一天按8小時480分鐘計算,周末不計,不考慮法定節假日。

                     -- 如果起始日期在周末,則計算當天時間。

                     DECLARE minutes FLOAT;

                     DECLARE next_day DATETIME;

                     SET minutes=0.0;

                     SET next_day=start_day;

                           

                     IF(TIMESTAMPDIFF(DAY,start_day,end_day)<1.0) THEN

                                   SET minutes=minutes+TIMESTAMPDIFF(MINUTE,start_day,end_day);

                     ELSE

                                   WHILE TIMESTAMPDIFF(DAY,next_day,end_day)>=1.0 DO

                                                 IF ((DAYOFWEEK(next_day)=7) OR (DAYOFWEEK(next_day)=1)) THEN

                                                               SET next_day=next_day+INTERVAL 1 DAY;

                                                 ELSE

                                                               SET next_day=next_day+INTERVAL 1 DAY;

                                                               SET minutes=minutes+480.0;

                                                 END IF;

                                   END WHILE;

                                   SET minutes=minutes+TIMESTAMPDIFF(MINUTE,next_day,end_day);

                                   IF ((DAYOFWEEK(start_day)=7) OR (DAYOFWEEK(start_day)=1)) THEN

                                                 SET minutes=minutes+TIMESTAMPDIFF(MINUTE,start_day,CONVERT(CONCAT(SUBSTRING(start_day FROM 1 FOR 10),' 17:30:00'),DATETIME));

                                   END IF;

                     END IF;

                     RETURN minutes;

END;

 沒考慮中午休息時間

 

摘自 Jasper鍵盤舞步

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved