Solved

Calculating age based on month

Posted on 2014-10-17
9
148 Views
Last Modified: 2014-10-17
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

Open in new window


How can I calculate based on "month" only?  Thanks for helping this novice sql person.
0
Comment
Question by:al4629740
  • 4
  • 3
  • 2
9 Comments
 
LVL 48

Expert Comment

by:PortletPaul
ID: 40387609
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
0
 

Author Comment

by:al4629740
ID: 40387621
No, the new constant will be the month

1
2
3
4
5
6
7
8
9
10
11
12
0
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 40387631
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  
    DOB < DATEADD(YEAR, -18, '20140701')
0
 

Author Comment

by:al4629740
ID: 40387644
No, I just want to calculate their age based on current MONTH and DOB
0
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.

 
LVL 69

Accepted Solution

by:
ScottPletcher earned 500 total points
ID: 40387647
DATEDIFF(MONTH, DOB, GETDATE()) / 12
0
 

Author Comment

by:al4629740
ID: 40387650
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
0
 
LVL 48

Expert Comment

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

Open in new window

line 2 above calculates "1st day of current month"
0
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 40387655
Yep, that should be it, if all you're worried about is the month of the dob.
0
 

Author Comment

by:al4629740
ID: 40387659
Yes, it would be comparing the month of DOB to month of whatever I specify
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Calculated columns 13 60
Changing SQL DB from Simple to Full recovery mode 3 46
Trouble viewing a table in a database. SSMS 2008 R2 4 31
T-SQL:  Embedding a CTE 5 31
When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

948 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now