MS-SQL: Query to find a age group?

Valleriani
Valleriani used Ask the Experts™
on
MSSQL 2008.. I have a table with data which has name, category, and date of birth.

The date of birth is, for example:

12/24/1970
11/10/1965

What I'm trying to do is find dates between, lets say, age 30-45 ... I guess I would do something like search between 1965-1980 of the current date (June 05th). Is this possible, and if so , how would this be handled in a query?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2012
Commented:
Something like this perhaps:
WHERE DOB BETWEEN DATEADD(year, -45, GETDATE()) AND DATEADD(year, -30, GETDATE())

Author

Commented:
Dateadd is working good except theres one issue.

When  a DOB includes 'time' for some reason, is there a way to ignore time? Like 12/12/1976 10:40  .. What is happening is IsDate() (To make sure theres a date there before hand) fails.
How to Generate Services Revenue the Easiest Way

This Tuesday! Learn key insights about modern cyber protection services & gain practical strategies to skyrocket business:

- What it takes to build a cloud service portfolio
- How to determine which services will help your unique business grow
- Various use-cases and examples

Author

Commented:
Actually its 5/14/2010 10:50 that is failing

Author

Commented:
Nevermind, got it :)
Top Expert 2012

Commented:
>>When  a DOB includes 'time' for some reason, is there a way to ignore time?<<
There are a couple of ways to handle that:
1. If you are using SQL Server 2008 than use DATE as in:
WHERE DOB BETWEEN DATEADD(year, -45, CAST(GETDATE() AS date)) AND DATEADD(year, -30, CAST(GETDATE() AS date))

2. If you are not using SQL Server 2008, use something like this:
WHERE DOB BETWEEN DATEADD(year, -45, DATEADD(day, 0, DATEDIFF(day, 0, GETDATE()))) AND DATEADD(year, -30, DATEADD(day, 0, DATEDIFF(day, 0, GETDATE())))

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial