sql server – How to get age

 

“datediff” function wont be helpful for calculating age in SQLSERVER.  Below query gives an idea on how to calculate age.

declare @bday datetime Set @bday = ‘2000-02-28 00:00:00.000’
declare @as_of datetime Set @as_of = ‘2002-03-01 00:00:00.000’

select
@bday,
@as_of,
Convert(Char(8),@as_of,112),
Convert(Char(8),@bday,112),
0 + Convert(Char(8),@as_of,112) – Convert(Char(8),@bday,112),
(0 + Convert(Char(8),@as_of,112) – Convert(Char(8),@bday,112)) / 10000

(CONVERT(INT, CONVERT(CHAR(8), @as_of, 112)) – CONVERT(CHAR(8), @bday, 112)) / 10000