温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

SQL中怎么计算工作日

发布时间:2021-08-07 16:24:38 来源:亿速云 阅读:340 作者:Leah 栏目:数据库

SQL中怎么计算工作日,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

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;

关于SQL中怎么计算工作日问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

sql
AI