Solved

Sql Server DATEDIFF - how to convert months to  YEARS , Months

Posted on 2014-10-08
1
235 Views
Last Modified: 2014-10-08
How do you convert months to  Years and Months?

SELECT DATEDIFF(month, '2003-12-31 23:59:59.9999999'
, '2006-01-01 00:00:00.0000000');    -- returns 25 months

I need to return    2 Years  and 1 Month
0
Comment
Question by:JElster
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 500 total points
ID: 40368908
SELECT CAST(DATEDIFF(month, '2003-12-31 23:59:59.9999999' , '2006-01-01 00:00:00.0000000')
    / 12 AS varchar(3)) + ' year(s) and ' +
    CAST(DATEDIFF(month, '2003-12-31 23:59:59.9999999' , '2006-01-01 00:00:00.0000000') % 12
    AS varchar(2)) + ' month(s)';
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Incorrect syntax near the keyword 'INNER' 3 38
VMware PVSCSI SQL Server 2016 AlwaysOn 2 37
When are cursors useful? 8 61
SQL Server replace between tags 6 27
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question