Ms Milliseconds
Yy Year
Qq Quarter of the Year
Mm Month
Dy The Day of the Year
Dd Day of the Month
Wk Week
Dw The Day of the Week
Hh Hour
Mi Minute
Ss Second
Example
Declare@dateofbirthdatetime
Declare@currentdatetimedatetime
Declare@daysvarchar(3)
set@dateofbirth='1986-03-15'--Birthdate
set@currentdatetime =getdate()--Current Datetime
select@days=datepart(d,@currentdatetime)-datepart(d,@dateofbirth)
select @days +' days' asDays
Output
Calculating Age in years, months and days
Here is an example to get the years, months and days between two dates.
Declare@dateofbirthdatetime
Declare@currentdatetimedatetime
Declare@yearsvarchar(40)
Declare@monthsvarchar(30)
Declare@daysvarchar(30)
set@dateofbirth='1986-03-15'--birthdate
set@currentdatetime =getdate()--current datetime
select@years=datediff(year,@dateofbirth,@currentdatetime)-- To find Years
select@months=datediff(month,@dateofbirth,@currentdatetime)-(datediff(year,@dateofbirth,@currentdatetime)*12)
-- To Find Months
select@days=datepart(d,@currentdatetime)-datepart(d,@dateofbirth)-- To Find Days
select@years +' years, ' +@months +' months, '+@days +' days' asYearMonthDay
Output