Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 372
  • Last Modified:

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

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
JElster
Asked:
JElster
1 Solution
 
Scott PletcherSenior DBACommented:
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

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now