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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:07:23+00:00 2026-06-11T21:07:23+00:00

I need to get the following result between two dates: date_start = 01/01/2010 date_end

  • 0

I need to get the following result between two dates:

date_start = 01/01/2010
date_end = 10/21/2012

result: 1 year, 9 months and 20 days.

I tried the code bellow, but it didn’t work. It returns negative dates sometimes:

SELECT CAST(DATEDIFF(yy, date_start, date_end) AS varchar(4)) +' year '+
       CAST(DATEDIFF(mm, DATEADD(yy, DATEDIFF(yy,date_start , date_end), date_start), date_end) AS varchar(2)) +' month '+
       CAST(DATEDIFF(dd, DATEADD(mm, DATEDIFF(mm, DATEADD(yy, DATEDIFF(yy, date_start, date_end), date_start), date_end), DATEADD(yy, DATEDIFF(yy, date_start, date_end), date_start)), date_end) AS varchar(2)) +' day' AS result

Thank You!

  • 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-11T21:07:24+00:00Added an answer on June 11, 2026 at 9:07 pm

    This may not correctly handle leap years if @s or @e are adjacent to them, but other than that this should be pretty close:

    DECLARE @s DATE, @e DATE
    
    SELECT @s = '20100101', @e = '20121021';
    
    SELECT y + ' year(s), ' + m + ' month(s) and ' + d + ' day(s).'
    FROM
    (
      SELECT 
        RTRIM(y), 
        RTRIM(m - CASE WHEN pd < 0 THEN 1 ELSE 0 END),
        RTRIM(CASE WHEN pd < 0 THEN nd ELSE pd END)
      FROM 
      (
        SELECT
          DATEDIFF(MONTH, @s, @e) / 12, 
          DATEDIFF(MONTH, @s, @e) % 12,
          DATEDIFF(DAY, @s, DATEADD(MONTH, -DATEDIFF(MONTH, @s, @e), @e)),
          DATEDIFF(DAY, @s, DATEADD(MONTH, 1-DATEDIFF(MONTH, @s, @e), @e))
      ) AS x (y, m, pd, nd)
    ) AS y (y, m, d);
    

    Output:

    2 year(s), 9 month(s) and 20 day(s).
    

    If accounting for the extra day in a leap year is crucial, I’m sure it could be adjusted to handle that. Though through minimal testing I wasn’t able to see any case where it would break (it just doesn’t feel like it should work).

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

Sidebar

Related Questions

I need to get my dates to be into the following format: 12/3/2011 for
I have the following xml that I need to get 2 values (see *
I have the following HTML and I need to get the first div parent
I'm stuck with the following. I'm programming a game and I need to get
I need to do the following and i was wondering if i could get
Wondering if I could get some advice and direction on this following requirement: Need
I have the following data transactions for Account Receivables: I need to get an
I want to get the difference between two images I use this source code:
I need to get all of the text contained between a specific div. In
I need get the id of an images which is place inside a datatemplate..

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.