How to calculate Age in Years and Months

Posted by Syedshakeer under Sql Server category on | Views : 5271
Below is the code in Sqlserver.
if you want to test it,just copy the code and paste it in your SqlSquery Analyzer and Run it.

DECLARE @setdate datetime
,@asof datetime
SET @setdate = '20070327'
SET @asof = GETDATE()
SELECT Years=DATEDIFF(year,@setdate,@asof)
- CASE WHEN MONTH(@asof)*100+DAY(@asof)<MONTH(@setdate)*100+DAY(@setdate) THEN 1 ELSE 0 END
,Months=( DATEDIFF(month,@setdate,@asof)
- CASE WHEN DAY(@asof)<DAY(@setdate) THEN 1 ELSE 0 END ) % 12

Comments or Responses

Login to post response