Click here to Skip to main content
15,906,708 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
what is sql query to get date diff in yrs and months..If no year it shouldn't be show and so for months...
Posted
Comments
joshrduncan2012 10-Apr-13 11:00am    
What have you tried so far to accomplish this?

try this query
SQL
SELECT
(CASE
     WHEN ( cast(DATEDIFF(Year, '2012-04-26', '2013-03-15') as int) = 0) THEN cast(DATEDIFF(MONTH, '2012-04-26', '2013-03-15')  as varchar) +' Month'
                                            ELSE cast(DATEDIFF(Year, '2011-04-26', '2013-03-15')as varchar) +'Years '+ cast(DATEDIFF(MONTH, '2012-04-26', '2013-03-15')as varchar) +'Month'  end) as diff;
 
Share this answer
 
v2
See if this helps:
SQL
select
    DATEDIFF(year, '1/1/1960', GETDATE()) AS Totalyears,
    DATEDIFF(month, '1/1/1960', GETDATE()) AS TotalMonths,
    DATEDIFF(month, '1/1/1960', GETDATE())/12 AS Years02,
    DATEDIFF(month, '1/1/1960', GETDATE())%12 AS RemainingMonths
 
Share this answer
 
Comments
Korathu 2 10-Apr-13 13:51pm    
Nothing worked...
Corporal Agarn 10-Apr-13 14:00pm    
This is just an example, not the answer. Which database server and version do you have?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900