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

  • SEARCH
  • Home
  • 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 8520527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T06:33:39+00:00 2026-06-11T06:33:39+00:00

I am trying to write a function in mySQL that takes two dates(startDate and

  • 0

I am trying to write a function in mySQL that takes two dates(startDate and endDate) as parameters. It then calculates the days in each month.

The database contains a targetRevenue table that has got the target revenue values for each month and year.

id  month   year  targetRev
25    1       2012    1000.00
26    2       2012    5000.00
27    3       2012    8000.00

The function finds the revenue for a month based on the number of days in it and then returns the total.

Example : startDate : 2012-01-19 endDate : 2012-03-24
Function returns [ targetRev(19 days in Jan) + targetRev(29 days Feb) + targetRev(24days in March)]

I am new to writing functions in mysql , so a little bit of help to get me started would be very useful. Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 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-06-11T06:33:40+00:00Added an answer on June 11, 2026 at 6:33 am

    If instead of your month and year columns, you represented the month of each record in your targetRevenue table by a DATE column containing the first day of each month:

    ALTER TABLE targetRevenue
      ADD COLUMN first DATE;
    
    UPDATE targetRevenue
      SET first = STR_TO_DATE(CONCAT_WS('-', year, month, 1), '%Y-%c-%e');
    
    ALTER TABLE targetRevenue
      DROP COLUMN year,
      DROP COLUMN month;
    

    You could then obtain the total target revenue for your project (assuming it is inclusive of both start and end date) with:

    -- calculate the summation of
    SELECT SUM(CONVERT(
    
             -- number of project days in month...
             GREATEST(0,
               -- ...is calculated as the difference between...
               DATEDIFF(
                 -- ...the last day of the project in this month...
                    LEAST('2012-03-24', LAST_DAY(first)),
                 -- ...and the first day of the project in this month...
                 GREATEST('2012-01-19', first)
               )
               -- ...plus one because first and last project days were inclusive
               + 1
             )
    
             -- multiply by the target revenue for this month
             * targetRev
    
             -- divide by the number of days in the month
             / DAY(LAST_DAY(first)),
    
             -- convert result to fixed-point format, to two d.p.
             DECIMAL(11,2)
    
           )) AS total
    
    FROM   targetRevenue
    
    -- only perform for months in which the project was active
    WHERE  '2012-01-19' <= LAST_DAY(first) AND first <= '2012-03-24'
    

    See it on sqlfiddle.

    If you can’t change the schema, you could replace references to first with the value to which that column was updated above.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write a function that can take three parameters: an SqlDataReader,
I am trying to write a function that will insert data into a MySQL
I'm trying to write a function that imports the mysql dump into the database.
I'm trying to write a php function that takes the $name and $time and
I'm trying to learn JSP/Servlets... I managed to write a function that takes a
I am trying to write a php function to stop MySQL injection attempts. What
I am trying write a function that generates simulated data but if the simulated
I am trying to write a function that will allow me to browse for
I'm trying to write a function that will: If there is no region selected,
I'm trying to write a stored function for MySQL to generate a random and

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.