Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Jan 04, 2013 01:45 PM by prontonet
Member
243 Points
484 Posts
Jan 01, 2013 05:10 PM|LINK
Hi,
I have a datable with a Field Birthday which is of Type DateTime and stores the Users Birth date.
On my aspx page, I want to display the users if there age is between 1 and 14 by calculating
their age getting the Current date time?
How would I write the SQL query and could I assign the resuls into one group?
Thank you
305 Points
73 Posts
Jan 01, 2013 05:14 PM|LINK
Please look this.Its helping more
http://social.msdn.microsoft.com/Forums/en/transactsql/thread/399a7e87-bc78-47a5-8cc2-2d2436886fd7
http://stackoverflow.com/questions/1572110/how-to-calculate-age-in-years-based-on-date-of-birth-and-getdate
Star
12092 Points
2035 Posts
Jan 01, 2013 07:54 PM|LINK
Try something like:
SELECT Name, Birthday FROM Users where Birthday between DATEADD(YEAR,-14, GETDATE()) AND DATEADD(Day,-365,Getdate())
Jan 01, 2013 09:56 PM|LINK
great - that works - could i also add a range ot the code?
for example just get the Users between 7 and 14 years old.
Jan 02, 2013 12:29 AM|LINK
Yes, in that case 7-14 years would be:
SELECT Name, Birthday FROM Users where Birthday between DATEADD(YEAR,-14, GETDATE()) AND DATEADD(YEAR,-7,Getdate())
Jan 02, 2013 12:06 PM|LINK
I assume it would be true to get people aged 18 to 30 it would be below ?
where Birthday between DATEADD(YEAR,-30, GETDATE()) AND DATEADD(YEAR,-18,Getdate())
Jan 02, 2013 12:56 PM|LINK
Although, I have not tested, that would be my assumption, also.
Jan 02, 2013 02:25 PM|LINK
great - With a similAR Query in SQL - is it possible to calculates the users AGE
based on their birthday so i can display it next to the birthdate column in a gridview?
Jan 02, 2013 05:57 PM|LINK
To calculate someone's age:
select Name, Birthdate, DATEDIFF(yy, Birthdate, GETDATE()) - CASE WHEN (MONTH(Birthdate) > MONTH(GETDATE())) OR (MONTH(Birthdate) = MONTH(GETDATE()) AND DAY(Birthdate) > DAY(GETDATE())) THEN 1 ELSE 0 END as Age FROM Birthday
prontonet
Member
243 Points
484 Posts
SELECT SQL QUERY USING CURRENT DATE TIMETO GET THE USERS AGE?
Jan 01, 2013 05:10 PM|LINK
Hi,
I have a datable with a Field Birthday which is of Type DateTime and stores the Users Birth date.
On my aspx page, I want to display the users if there age is between 1 and 14 by calculating
their age getting the Current date time?
How would I write the SQL query and could I assign the resuls into one group?
Thank you
0ramramram0
Member
305 Points
73 Posts
Re: SELECT SQL QUERY USING CURRENT DATE TIMETO GET THE USERS AGE?
Jan 01, 2013 05:14 PM|LINK
Please look this.Its helping more
http://social.msdn.microsoft.com/Forums/en/transactsql/thread/399a7e87-bc78-47a5-8cc2-2d2436886fd7
http://stackoverflow.com/questions/1572110/how-to-calculate-age-in-years-based-on-date-of-birth-and-getdate
paindaasp
Star
12092 Points
2035 Posts
Re: SELECT SQL QUERY USING CURRENT DATE TIMETO GET THE USERS AGE?
Jan 01, 2013 07:54 PM|LINK
Try something like:
prontonet
Member
243 Points
484 Posts
Re: SELECT SQL QUERY USING CURRENT DATE TIMETO GET THE USERS AGE?
Jan 01, 2013 09:56 PM|LINK
great - that works - could i also add a range ot the code?
for example just get the Users between 7 and 14 years old.
paindaasp
Star
12092 Points
2035 Posts
Re: SELECT SQL QUERY USING CURRENT DATE TIMETO GET THE USERS AGE?
Jan 02, 2013 12:29 AM|LINK
Yes, in that case 7-14 years would be:
prontonet
Member
243 Points
484 Posts
Re: SELECT SQL QUERY USING CURRENT DATE TIMETO GET THE USERS AGE?
Jan 02, 2013 12:06 PM|LINK
I assume it would be true to get people aged 18 to 30 it would be below ?
paindaasp
Star
12092 Points
2035 Posts
Re: SELECT SQL QUERY USING CURRENT DATE TIMETO GET THE USERS AGE?
Jan 02, 2013 12:56 PM|LINK
Although, I have not tested, that would be my assumption, also.
prontonet
Member
243 Points
484 Posts
Re: SELECT SQL QUERY USING CURRENT DATE TIMETO GET THE USERS AGE?
Jan 02, 2013 02:25 PM|LINK
great - With a similAR Query in SQL - is it possible to calculates the users AGE
based on their birthday so i can display it next to the birthdate column in a gridview?
prontonet
Member
243 Points
484 Posts
Re: SELECT SQL QUERY USING CURRENT DATE TIMETO GET THE USERS AGE?
Jan 02, 2013 02:25 PM|LINK
great - With a similAR Query in SQL - is it possible to calculates the users AGE
based on their birthday so i can display it next to the birthdate column in a gridview?
paindaasp
Star
12092 Points
2035 Posts
Re: SELECT SQL QUERY USING CURRENT DATE TIMETO GET THE USERS AGE?
Jan 02, 2013 05:57 PM|LINK
To calculate someone's age: