IonicWind Software

IWBasic => Database => Topic started by: Brian on June 18, 2009, 03:38:57 AM

Title: Selecting Records by SQL Query
Post by: Brian on June 18, 2009, 03:38:57 AM
Hi,

I have a database of names and addresses with dates of birth

I need to select records where age is not over 12 when the
record is selected. So, given a start date and an end date,
any selected record's age would not be over 12 by the
end date

Any SQL ideas?

Thanks a lot,

Brian
Title: Re: Selecting Records by SQL Query
Post by: friet on June 18, 2009, 04:47:20 PM
Hi Brian,

I assume it does depend on the version of SQL you're using, but in MS SQL you can do :

"SELECT * from <tablename> where (year(curdate())-year(date-of-birth))<=12"

while not completely exact, this should give you a clue how to continue (because this doesn't take into account the month/day)
You would probably need to write a stored function to have a correct calculation  (and leave it out of your emergence code, so it gets executed on the server, probably much faster)

hope this helps..

regards,

Frank

Title: Re: Selecting Records by SQL Query
Post by: Brian on June 19, 2009, 11:59:05 AM
Frank,

Thanks for your reply - this gives me a great starting point. I'll give it a go
tonight

Brian
Title: Re: Selecting Records by SQL Query
Post by: Allan on June 19, 2009, 05:11:26 PM
Brian

Here is some code I have used with the Easy Database (MS ACCESS)

You just need to put your dates in as needed.

Hope it is of some help.

Allan