Age in years and months

Submitted by:Pitter Alderson

Date added:16 January, 2013

Category:SQL

How to calculate age in years and months

Tags: calculate age

Code Snippet:

--create a table hermp
create table hremp (emid int ,
dob datetime)
--insert records into table
insert into hremp values ('1','1965-06-10 00:00:00.000')
insert into hremp values ('2','1981-05-21 00:00:00.000')
insert into hremp values ('3','1981-05-11 00:00:00.000')
--query to find the age of an employee
select EMID,DOB,DATEDIFF(DD,dob,getdate())/365 as years,
(DATEDIFF(DD,dob,getdate())%365)/30 as months
from HREMP
 
 

Comments