Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hello everyone,
How can i convert number of months to number of year??
e.g: if i have 36 months then i should get 3 year.. is it possible?
if so please suggest me...

"Thanking you"
Posted

1 solution

SQL
declare @year int
declare @months int
set @months=36
set @year=@months/12
print @year


In the above case, @year is an integer, so even if you pass 45 as month, you will get 3 as output.

So it will be like
0-11 out put is 0
12-23 output is 1
24-35 output is 2
36-47 output is 3

Updated solution

SQL
declare @year int
declare @months int
set @months=45
set @year=@months/12
set @months=@months%12
print cast(@year as varchar(100))+' years '+cast(@months as varchar(100))+ ' months '


this wil give me
3 years 9 months
 
Share this answer
 
v3
Comments
Gopal Rakhal 25-Aug-12 3:14am    
thanks, but if i have 45 months then it will give 3.75 year, so what to do in this case.....
Santhosh Kumar Jayaraman 25-Aug-12 3:17am    
In that case what you want 3 or 4?
Gopal Rakhal 25-Aug-12 3:42am    
I need 3year 9months.. is it possible in sql or i need to write coding?
Santhosh Kumar Jayaraman 25-Aug-12 3:45am    
Check my updated solution

declare @year int
declare @months int
set @months=45
set @year=@months/12
set @months=@months%12
print cast(@year as varchar(100))+' years '+cast(@months as varchar(100))+ ' months '
Gopal Rakhal 25-Aug-12 4:11am    
thanks,

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