List: General Discussion « Previous MessageNext Message » From: Paul DuBois Date: March 30 2002 11:59pm Subject: RE: Calculate current age with given birthday View as plain text
```At 17:15 -0500 3/30/02, Michael Stassen wrote:
>On Sat, 30 Mar 2002, Paul DuBois wrote:
>
>>  At 21:25 +0100 3/30/02, Roger Baklund wrote:
>>  >* Son Nguyen
>>  >>  ==> I tried this query, but... it's not working out:
>>  >>    SELECT YEAR(now()-birthday) from users where loginName="test";
>>  >
>>  >You are very close:
>>  >
>>  >SELECT YEAR(now())-YEAR(birthday) as age from users where loginName="test";
>>
>>  That won't adjust for the relative difference within the calendar
>>  year of the two dates.  This works better:
>>
>>  mysql> SELECT
>>       -> YEAR(CURDATE()) - YEAR(birthday)
>>       -> - IF(RIGHT(CURDATE(),5) < RIGHT(birthday,5),1,0)
>>       -> AS 'age in years'
>>       -> FROM users WHERRE loginName="test";
>
>Or
>
>   SELECT
>     YEAR(FROM_DAYS(TO_DAYS(CURDATE()) - TO_DAYS(birthday)))
>     AS 'age in years'
>     FROM users WHERE loginName='test';
>
>Michael

mysql> SET @birth = CURDATE() - INTERVAL 1 YEAR;
mysql> SELECT
->     CURDATE(), @birth,
->     YEAR(FROM_DAYS(TO_DAYS(CURDATE()) - TO_DAYS(@birth)))
->     AS 'age in years';
+------------+------------+--------------+
| CURDATE()  | @birth     | age in years |
+------------+------------+--------------+
| 2002-03-30 | 2001-03-30 |            0 |
+------------+------------+--------------+
mysql> SELECT
->     CURDATE(), @birth,
->     YEAR(CURDATE()) - YEAR(@birth)
->     - IF(RIGHT(CURDATE(),5) < RIGHT(@birth,5),1,0)
->     AS 'age in years';
+------------+------------+--------------+
| CURDATE()  | @birth     | age in years |
+------------+------------+--------------+
| 2002-03-30 | 2001-03-30 |            1 |
+------------+------------+--------------+
```
LIMIT, ORDER Dilemadestr030 Mar
• Re: LIMIT, ORDER DilemaTyler Longren30 Mar
• Re: LIMIT, ORDER DilemaPaul DuBois30 Mar
Re: LIMIT, ORDER Dilemadestr030 Mar
• Re: LIMIT, ORDER DilemaPaul DuBois30 Mar
Re: LIMIT, ORDER Dilemadestr030 Mar
• Re: LIMIT, ORDER DilemaPaul DuBois30 Mar
• RE: LIMIT, ORDER DilemaRoger Baklund30 Mar
• RE: LIMIT, ORDER DilemaPaul DuBois30 Mar
• Calculate current age with given birthdaySon Nguyen30 Mar
• RE: Calculate current age with given birthdayRoger Baklund30 Mar
• RE: Calculate current age with given birthdayPaul DuBois30 Mar
• RE: Calculate current age with given birthdayMichael Stassen30 Mar
Re: LIMIT, ORDER DilemaSteven Roussey30 Mar
RE: Calculate current age with given birthdayPaul DuBois31 Mar
• RE: Calculate current age with given birthdayMichael Stassen2 Apr