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 7689525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:11:06+00:00 2026-05-31T20:11:06+00:00

This is what I would like to be able to do: SET @interval_type :=

  • 0

This is what I would like to be able to do:

SET @interval_type := MONTH;
SELECT '2012-01-01' + INTERVAL 6 @interval_type;
+------------+
|'2012-06-01'|
+------------+

And of course that doesn’t work and there is no “interval” data type in MySQL.

I want to be able to store an interval value and an interval type in a table so that i can have the database quickly do the math naturally without having to write a big switch statement, ala

... ELSE IF (type = 'MONTH') { SELECT @date + INTERVAL @value MONTH; } ... 

Is this supported in any way in MySQL or do you have a clever hack for this?

Thanks; you rock.

  • 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-05-31T20:11:07+00:00Added an answer on May 31, 2026 at 8:11 pm

    Here’s the simplistic approach. It works reasonably fast. You can change the order of the switch statements to optimize for speed if you feel that you will be hitting some more often then others. I have not benched this against Chris Hutchinson’s solution. I ran into problems trying to wrap it into a nice function because of the dynamic SQL. Anyway, for posterity, this is guaranteed to work:

    CREATE FUNCTION AddInterval( date DATETIME, interval_value INT, interval_type TEXT  ) 
    RETURNS DATETIME
    DETERMINISTIC
    BEGIN
        DECLARE newdate DATETIME;
        SET newdate = date;
    
        IF interval_type = 'YEAR' THEN
            SET newdate = date + INTERVAL interval_value YEAR;
        ELSEIF interval_type = 'QUARTER' THEN
            SET newdate = date + INTERVAL interval_value QUARTER;
        ELSEIF interval_type = 'MONTH' THEN
            SET newdate = date + INTERVAL interval_value MONTH;
        ELSEIF interval_type = 'WEEK' THEN
            SET newdate = date + INTERVAL interval_value WEEK;
        ELSEIF interval_type = 'DAY' THEN
            SET newdate = date + INTERVAL interval_value DAY;
        ELSEIF interval_type = 'MINUTE' THEN
            SET newdate = date + INTERVAL interval_value MINUTE;
        ELSEIF interval_type = 'SECOND' THEN
            SET newdate = date + INTERVAL interval_value SECOND;
        END IF;
    
        RETURN newdate;
    END //
    

    It comes with this equally simplistic benchmark test:

    CREATE FUNCTION `TestInterval`( numloops INT ) 
    RETURNS INT
    DETERMINISTIC
    BEGIN
        DECLARE date DATETIME;
        DECLARE newdate DATETIME;
        DECLARE i INT; 
        SET i = 0;
    
        label1: LOOP
            SET date = FROM_UNIXTIME(RAND() * 2147483647);
            SET newdate = AddInterval(date,1,'YEAR');
            SET i = i+1;
            IF i < numloops THEN
                ITERATE label1;
            ELSE
                LEAVE label1;
            END IF;
        END LOOP label1;
        return i;
    END //
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i would like to be able to ensure that if a table (in this
I would like to be able to achieve something like this: class Zot {
I would like to be able to switch this... My sample [a id=keyword href=someURLkeyword]
This is quite complicated but i would like to be able to refresh a
This is really just for my own use: I would like to be able
Hope this is not a dupe. I would like to be able to do
I have this code, which works fine, but I would like to be able
I would like to see how this example of existing code would be able
I am wondering what it would take to make something like this work: using
I would like to setup a multi-tenant ASP.NET MVC app. Ideally, this app would

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.