SQL for workday computing

Source: Internet
Author: User

 

Create function Time_Span_of_minutes (

Start_day DATETIME,

End_day DATETIME)

RETURNS FLOAT

BEGIN

-- Returns the interval between two periods calculated by minute. It is calculated by the method of approaching each day. The value is calculated by 8 hours and 480 minutes per day. It is not counted on weekends and does not take legal holidays into account.

-- If the start date is on the weekend, the current day is counted.

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

-- Returns the interval between two periods calculated by minute. It is calculated by the method of approaching each day. The value is calculated by 8 hours and 480 minutes per day. It is not counted on weekends and does not take legal holidays into account.

-- If the start date is on the weekend, the current day is counted.

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;

No noon break

 

From Jasper keyboard dance steps

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.