Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 729663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:49:21+00:00 2026-05-14T06:49:21+00:00

I just wrote a stored function to calculate the working days between two dates.

  • 0

I just wrote a stored function to calculate the working days between two dates.
This works

select
CountWeekDays(‘2010-03-07′,’2010-04-07’)

This doesn’t work

select
CountWeekDays(o.order_date,o.created_date)
from orders o;

Any idea how to make this one work ??

function definition

delimiter $$;

CREATE FUNCTION CountWeekDays (sdate
VARCHAR(50), edate VARCHAR(50))
RETURNS INT

BEGIN

DECLARE wdays, tdiff, counter, thisday
smallint;

DECLARE newdate DATE;

SET newdate := sdate;

SET wdays = 0;

if DATEDIFF(edate, sdate) = 0 THEN
RETURN 1; END IF;

if DATEDIFF(edate, sdate) < 0 THEN
RETURN 0; END IF;

label1: LOOP

SET thisday = DAYOFWEEK(newdate);

IF thisday BETWEEN 2 AND 6 THEN SET
wdays := wdays + 1; END IF;

SET newdate = DATE_ADD(newdate,
INTERVAL 1 DAY);

IF DATEDIFF(edate, newdate) < 0 THEN
LEAVE label1; END IF;

END LOOP label1;

RETURN wdays;

END

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T06:49:21+00:00Added an answer on May 14, 2026 at 6:49 am

    It had something to do with datatype .. following function worked fine ..

    SET GLOBAL log_bin_trust_function_creators=1;
    DROP FUNCTION IF EXISTS BizDaysInclusive;
    DELIMITER | 
    CREATE FUNCTION BizDaysInclusive( d1 DATE, d2 DATE ) 
    RETURNS INT 
    DETERMINISTIC 
    BEGIN 
    DECLARE dow1, dow2, days INT;
    SET dow1 = DAYOFWEEK(d1);
    SET dow2 = DAYOFWEEK(d2);
    SET days = FLOOR( DATEDIFF(d2,d1)/7 ) * 5 + 
    CASE
    WHEN dow1=1 AND dow2=7 THEN 5 
    WHEN dow1 IN(7,1) AND dow2 IN (7,1) THEN 0
    WHEN dow1=dow2 THEN 1 
    WHEN dow1 IN(7,1) AND dow2 NOT IN (7,1) THEN dow2-1 
    WHEN dow1 NOT IN(7,1) AND dow2 IN(7,1) THEN 7-dow1
    WHEN dow1<=dow2 THEN dow2-dow1+1
    WHEN dow1>dow2 THEN 5-(dow1-dow2-1)
    ELSE 0
    END;
    RETURN days;
    END;
    |
    DELIMITER ; 
    
    SELECT BizDaysInclusive('2009-8-1','2009-9-15');
    
    select substr(o.order_date,1,10) a ,substr(o.created_date,1,10) b, CountWeekDays(substr(o.order_date,1,10),substr(o.created_date,1,10)) c from orders o WHERE o.order_date like '2010-04-08%' ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 508k
  • Answers 508k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Why not use mysql_insert_id() ? As for your question if… May 16, 2026 at 4:14 pm
  • Editorial Team
    Editorial Team added an answer Are you sure you're using suExec PHP? If it's just… May 16, 2026 at 4:13 pm
  • Editorial Team
    Editorial Team added an answer From the developer page: "Returns the X coordinate of this… May 16, 2026 at 4:13 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Well I have this MySQL stored procedure that I wrote and if I run
I'm a mechanical engineering grad student and my adviser has just asked me to
I want to create a decorator that works like a property, only it calls
I am trying to write a simple function for windows that answers the following
I am trying to have the function toggleClick return powerData. I need to store
I'm supposed to take two sentences each time and compute if they are similar.
I am in trouble, so need some help. I have my file stored in
I am working on a search web page for my site. The requirements state
A project I have been working on for the past year writes out log
I'm writing reports on SQL Server Reporting Server that have a number of hours

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.