Solved

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

Posted on 2014-10-08
1
208 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
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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

809 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