Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
Classic ASP Databases Discuss using ASP 3 to work with data in databases, including ASP Database Setup issues from the old P2P forum on this specific subtopic. See also the book forum Beginning ASP.NET Databases for questions specific to that book. NOT for ASP.NET 1.0, 1.1, or 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Classic ASP Databases section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old August 4th, 2004, 06:15 PM
Authorized User
Join Date: Aug 2004
Location: , , .
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default Calculating and filtering age based on date

I have a SQL DB with a table that contains a "YEAR" column. I also have a ASP.NET web form with two drop down fields, looks like this:

AGE: Between [18] and [25]

[18] and [25] being drop down boxes

I would like to know what SQL math funciton in combination with the TSQL date functions I can use to calculate and filter this data. I also have a MONTH column and a DAY column that would need to be included in the query to produce an accurate result, i guess.


-- shawn

  #2 (permalink)  
Old August 5th, 2004, 07:40 AM
Friend of Wrox
Join Date: Jun 2003
Location: Harrisburg, PA, USA.
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts


What result do you want to produce? Do you want to see who was born between those age limits? If so, subtract 18 and 25 from the year to find the year range, and select everyone in between.

  #3 (permalink)  
Old August 5th, 2004, 08:49 AM
Friend of Wrox
Join Date: Jun 2003
Location: Bangalore, KA, India.
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post


It would always be tedious for you to produce that exact result you are looking for with this kind of setup. As SQL itself has a DATETIME datatype, from which you can produce this exact result with less effort. I feel you have complicated things by having the Year/Month/Day as different columns. Merge them together, you can easily achieve that.
Select * from TableName where datediff(yy,AgeColumnName, getdate()) BETWEEN 18 and 25
Is that NOT so easy?

Else you will have to do all that calculations yourself from each year and then month and then day from your table... that is something really I can't think about.

Hope this helps.

- Vijay G
Strive for Perfection
  #4 (permalink)  
Old August 5th, 2004, 12:11 PM
Authorized User
Join Date: Aug 2004
Location: , , .
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts

DateDiff; those SQL pioneers have though of everything. That function is exactly what I was looking for. Thanks Vijay!

  #5 (permalink)  
Old November 12th, 2005, 06:08 AM
Registered User
Join Date: Nov 2005
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts

-- Be careful with DateDiff yy - that just returns the years between two dates and *not* elapsed time. Consider:

SELECT DateDiff(yy,'12/31/2000','1/1/2005') AS YearDiff, FLOOR(DateDiff(dd,'12/31/2000','1/1/2005')/365.25) As CloserToCorrect

YearDiff returns 5, 2005-2000. This answer is incorrect, our example person just had their 4th birthday the day before.
The CloserToCorrect field gets us a lot closer to an accurate age of 4.
Date / Age
12/31/2000 / 0
12/31/2001 / 1
12/31/2002 / 2
12/31/2003 / 3
12/31/2004 / 4
12/31/2005 / 5

Similar Threads
Thread Thread Starter Forum Replies Last Post
Filtering the list between the date range sea_aiko SharePoint Development 1 August 7th, 2008 04:21 PM
filtering date in Sharepoint sea_aiko ActionScript 0 April 4th, 2008 12:07 PM
Select fields & calculating age miki-pt Access ASP 1 November 26th, 2006 06:16 PM
calculating a persons age Hudson40 Access VBA 4 January 26th, 2005 11:45 AM
Calculating a field based on a lookup range nrwatson Access VBA 1 April 29th, 2004 06:41 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.