Solved

current age

Posted on 2014-10-10
6
103 Views
Last Modified: 2014-10-10
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
Comment
Question by:al4629740
  • 3
  • 2
6 Comments
 
LVL 40

Expert Comment

by:Kyle Abrahams
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 48

Expert Comment

by:PortletPaul
ID: 40374207
Please try this:
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
             

Open in new window


+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

by:al4629740
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
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 48

Accepted Solution

by:
PortletPaul earned 500 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
             

Open in new window


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'

then replace getdate() with @adate
0
 

Author Comment

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

Expert Comment

by:PortletPaul
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SSRS 2013 - Creating a summarized report 19 32
what are the unique tables in SQL master database 5 58
Can > be used for a Text field 6 37
SQL View nearest date 5 35
I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question