# Calculating age based on month

If I am calculating age based on "date" using this query

``````select *
from tblregistrations
WHERE  YEAR('20140701') - YEAR(DOB)
- ( CASE
WHEN MONTH(DOB) > MONTH('20140701')
OR ( MONTH(DOB) = MONTH('20140701')
AND DAY(DOB) > DAY('20140701')
)
THEN 1
ELSE 0
END )  < 18
``````

How can I calculate based on "month" only?  Thanks for helping this novice sql person.
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

please provide an example of what you mean by "month only"

is the DOB "month only"?
are you still comparing to the constant '20140701'?
an example would really help
Author Commented:
No, the new constant will be the month

1
2
3
4
5
6
7
8
9
10
11
12
Senior DBACommented:
If you want to see if a person is less than a certain age, you can just calculate the "controlling date", and then test for the DOB < that.  Something like this:

select *
from tblregistrations
WHERE
Author Commented:
No, I just want to calculate their age based on current MONTH and DOB
Senior DBACommented:
DATEDIFF(MONTH, DOB, GETDATE()) / 12

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:
So I can basically stick whatever date I am comparing to in the GETDATE() section?

For example:

DATEDIFF(MONTH, DOB, GETDATE()) / 12
or
DATEDIFF(MONTH, DOB, '12/1/2014') / 12
or
DATEDIFF(MONTH, DOB, variable) / 12
So "current month" can be treated as "1st of" the current month, and the calculation method remains the same.

``````declare @datum as date
set @datum = (select DATEADD(dd, - (DAY(getdate()) - 1), cast(getdate() as date) ) )

select *
from tblregistrations
WHERE  YEAR(@datum) - YEAR(DOB)
- ( CASE
WHEN MONTH(DOB) > MONTH(@datum)
OR ( MONTH(DOB) = MONTH(@datum)
AND DAY(DOB) > DAY(@datum)
)
THEN 1
ELSE 0
END )  < 18
``````
line 2 above calculates "1st day of current month"
Senior DBACommented:
Yep, that should be it, if all you're worried about is the month of the dob.
Author Commented:
Yes, it would be comparing the month of DOB to month of whatever I specify
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.