Click here to Skip to main content
15,077,494 members
Please Sign up or sign in to vote.
1.22/5 (2 votes)
See more:
Need to calculate DOB month and age from given date of birth.

DOB:
1988-10-20
(YYYY-MM-DD)

I need output as

10 and age of(1988-10-20) 


I have tried using it, but i coudn't get proper output.

SELECT (DATEDIFF(MONTH,'1988-10-20',getdate()))-(DATEDIFF(YEAR,'1988-10-20',GETDATE())*12)  AS Smonth


But iam getting output as -1,

I need to get output as 10.

What I have tried:

SELECT (DATEDIFF(MONTH,'1988-10-20',getdate()))-(DATEDIFF(YEAR,'1988-10-20',GETDATE())*12)  AS Smonth
Posted
Updated 26-Sep-18 6:53am
v3

See Solution 2 to this post How to calculate age from date of birth in sql[^]
   
SQL
DECLARE @DOB Date='07/03/1993';

SELECT CAST(ROUND((DATEDIFF(DAY,@DOB,GETDATE())/365.0),0) AS INT) AS AGE

OUTPUT:-
----
AGE
----
25
   
v2
select convert(nvarchar(max),Month('1988-10-20'))+' and ' + convert(nvarchar(max), DATEDIFF(year, '1988-10-20',getdate()))+' age of '+'1988-10-20' as Age 


Output
----------------
10 and 30 age of 1988-10-20
   
v2
To calculate month from given date

Declare @DOB datetime='1988-10-20'
select Month(@DOB) [Month]
   

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