Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# current age

Posted on 2014-10-10
Medium Priority
107 Views
I need two examples of current age in sql.  How can I select all the people in my tblregistrations who are currently 17 and under based on month and also based on date?

Does this work for month?
select * from tblregistrations where FLOOR(DATEDIFF(day, DOB, Month(servertime)) / 365.25) < 18

Does this work for date?
select * from tblregistrations where FLOOR(DATEDIFF(day, DOB, '7/1/2014') / 365.25) < 18
0
Question by:al4629740
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 3
• 2

LVL 40

Expert Comment

ID: 40374205
you would need month and year to calculate age.  Month in and of itself tells you nothing.

the 365.25 also doesn't work.  I know you're trying to compensate for leap year but you'll get into rounding issues.

What exactly are you trying to do?
0

LVL 49

Expert Comment

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

``````

+edit:
absolutely agree with Kyle anything based on 365.25 is NOT a reliable method
see: MSSQL: Calculating Age (in years) :: right -v- wrong
0

Author Comment

ID: 40374263
There are two different sql statements that I'm working on.

I am trying to retrieve the records for everyone less than 18 based on the servertime for the one.

For the second query, I am trying to get all less than 18 based on a specified DATE. ie 7/1/2014
0

LVL 49

Accepted Solution

PortletPaul earned 2000 total points
ID: 40374276
``````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

``````

to me 7/1/2014 = 7th Jan 2014, so I see it in YYYYMMDD as this: '20140107'
I suspect you really want '20140701'

just replace
getdate()

with
'20140701'

OR

declare @adate as date = '20140701'

0

Author Comment

ID: 40374286
Curiously, how much extra "query time" does it take to run these type of calculated queries?  Is it heavy?
0

LVL 49

Expert Comment

ID: 40374312
It's not an ideal filter condition and it will add some effort; how much "slowness" that introduces I cannot say.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have a large data set and a SSIS package. How can I load this file in multi threading?
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…
###### Suggested Courses
Course of the Month7 days, 3 hours left to enroll