Count the number of working days between the current date and the end of the month

Source: Internet
Author: User


Count the number of working days between the current date and the end of the month. If you have a requirement on the Operation side today, you need to find out the registration users who want to exclude the daily logging on Saturday and Sunday (including not the day of Saturday ), at that time, the first response was to find a function that could exclude the number of working days that can be calculated by the end of the month on Saturday and Sunday. If I hadn't found it for half a day, I 'd like to write it myself. I also wrote one of my favorite Big Brother Wang and posted it to share it with you. The Code is as follows: www.2cto.com drop function if exists count_day_left; DELIMITER // create function count_day_left (f_date DATETIME) returns int deterministic begin/* Purpose: counts the number of working days before the end of a specified date, the filtering function Useage for logging statistics is: select count_day_left ('2017-09-10 '); */DECLARE start_day INT; DECLARE end_day INT; DECLARE count_day int default 0; DECLARE tmp_date date default date (f_date); SET end_day = DAY (LAST_DAY (f_date); SET start_day = DAY (f_date ); WHILE start_day <= end_day do if (DAYOFWEEK (tmp_date) = 1) OR (DAYOFWEEK (tmp_date) = 7) then set tmp_date = DATE_ADD (tmp_date, INTERVAL 1 DAY ); SET start_day = start_day + 1; else set tmp_date = DATE_ADD (tmp_date, INTERVAL 1 DAY); SET start_day = start_day + 1; SET count_day = count_day + 1; end if; end while; RETURN count_day; END // DELIMITER; Revoke drop function if exists count_day_left; DELIMITER // create function count_day_left (f_date DATE) returns intreads SQL DATABEGINRETURN DATEDIFF (LAST_DAY (f_date), f_date) -(WEEK (LAST_DAY (f_date)-WEEK (f_date) * 2 + IF (WEEKDAY (f_date) = 6, 0, 1)-IF (WEEKDAY (LAST_DAY (f_date )) = 5, 1, 0); END // DELIMITER;

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.