How to get difference in year month and days from 2 date values ?

Following query will give result as (X Years X Months X Days) :

Declare @DOB DateTime
DECLARE @yy INT
DECLARE @mm INT
DECLARE @dd INT

SET @DOB=’2011-10-11 00:00:00.000′
SET @yy = DATEDIFF(mm, @DOB, GETDATE())/12
SET @mm = DATEDIFF(mm, @DOB, GETDATE())%12 – 1
SET @dd = ABS(DATEDIFF(dd, DATEADD(mm,@mm , DATEADD(yy, @yy, @DOB)), GETDATE()))
SELECT Convert(varchar(10),@yy) + ‘ Years ‘ + Convert(varchar(10),@mm) + ‘ Months ‘ + Convert(varchar(10),@dd) + ‘ Days ‘

There are alternate ways also to achieve this.Share your ideas.

Reference: Narendra Singh (http://blog.sikarnarender.com)

Leave a Reply

Your email address will not be published. Required fields are marked *