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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:59:07+00:00 2026-05-26T03:59:07+00:00

I want to get the difference in years from two different dates using MySQL

  • 0

I want to get the difference in years from two different dates using MySQL database.

for example:

  • 2011-07-20 – 2011-07-18 => 0 year
  • 2011-07-20 – 2010-07-20 => 1 year
  • 2011-06-15 – 2008-04-11 => 2 3 years
  • 2011-06-11 – 2001-10-11 => 9 years

How about the SQL syntax? Is there any built in function from MySQL to produce the result?

  • 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-26T03:59:07+00:00Added an answer on May 26, 2026 at 3:59 am

    Here’s the expression that also caters for leap years:

    YEAR(date1) - YEAR(date2) - (DATE_FORMAT(date1, '%m%d') < DATE_FORMAT(date2, '%m%d'))
    

    This works because the expression (DATE_FORMAT(date1, '%m%d') < DATE_FORMAT(date2, '%m%d')) is true if date1 is “earlier in the year” than date2 and because in mysql, true = 1 and false = 0, so the adjustment is simply a matter of subtracting the “truth” of the comparison.

    This gives the correct values for your test cases, except for test #3 – I think it should be “3” to be consistent with test #1:

    create table so7749639 (date1 date, date2 date);
    insert into so7749639 values
    ('2011-07-20', '2011-07-18'),
    ('2011-07-20', '2010-07-20'),
    ('2011-06-15', '2008-04-11'),
    ('2011-06-11', '2001-10-11'),
    ('2007-07-20', '2004-07-20');
    select date1, date2,
    YEAR(date1) - YEAR(date2)
        - (DATE_FORMAT(date1, '%m%d') < DATE_FORMAT(date2, '%m%d')) as diff_years
    from so7749639;
    

    Output:

    +------------+------------+------------+
    | date1      | date2      | diff_years |
    +------------+------------+------------+
    | 2011-07-20 | 2011-07-18 |          0 |
    | 2011-07-20 | 2010-07-20 |          1 |
    | 2011-06-15 | 2008-04-11 |          3 |
    | 2011-06-11 | 2001-10-11 |          9 |
    | 2007-07-20 | 2004-07-20 |          3 |
    +------------+------------+------------+
    

    See SQLFiddle

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

Sidebar

Related Questions

What I want to do is get the data from two different tables (table1
I want to get the difference between two sets of ints in c#. Given
I have a string pc1|pc2|pc3| I want to get each word on different line
i want to access or get the class which is present in different project.How
I want to get started doing some game development using Microsoft's XNA. Part of
After years of using chmod 777 to solve PHP write permission woes, I want
If I get records from my SQLite database's master table like this: $database =
How can I get all differences, not just one? I want to use the
I want get the time used for a case so I can create an
I want get all of the Geom objects that are related to a certain

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.