0

This is my MySQL Query to return the age from the date of birth

SELECT
    PensionerDOB,
    YEAR( CURDATE() ) AS Year, 
    DATE_FORMAT( STR_TO_DATE( PensionerDOB, '%d-%M-%Y' ), '%Y') AS age,
    
    YEAR( CURDATE() ) - DATE_FORMAT( STR_TO_DATE(`PensionerDOB`, '%d-%M-%Y' ), '%Y' ) AS differenage 

FROM
    `pensionerbasicdata`

The query is executed. But it returns the age difference is in a negative value.

Output

Dai
  • 141,631
  • 28
  • 261
  • 374
Mukhila Asokan
  • 641
  • 3
  • 11
  • 29
  • 1
    Why are you using `STR_TO_DATE` and `DATE_FORMAT` _together_? You don't need to do that. – Dai Dec 16 '21 at 06:00
  • 1
    If someone is born in 1945, they are not 2045 years old... Please use more accurate column names. – Dai Dec 16 '21 at 06:00
  • I could be wrong here, but can't you reverse the minuend and subtrahend to reverse the sign (if the magnitude is correct)? – user904963 Dec 16 '21 at 06:03
  • @Dai DOB is in the varchar format, So varchar convert to the date format – Mukhila Asokan Dec 16 '21 at 06:08
  • 1
    @Asokan Storing dates as strings is _wrong_. Why aren't you storing dates in a `date` column? – Dai Dec 16 '21 at 06:22

3 Answers3

1
SELECT *,
       TIMESTAMPDIFF(year, STR_TO_DATE(CONCAT(SUBSTRING_INDEX(PensionerDOB, '-', 2), '-19', SUBSTRING_INDEX(PensionerDOB, '-', -1)), '%d-%M-%Y'), CURRENT_DATE) AS age
FROM pensionerbasicdata

The problem with 2-digit year fixed - all years are treated as 19xx.

https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=f356258c99b20d13b0c4e2349b801f18

Akina
  • 39,301
  • 5
  • 14
  • 25
  • Fixed? By treating all twenty year olds as hundred and twenty year olds? It is `TIMESTAMPDIFF(year,` by the way, i.e. no quotes around year. – Thorsten Kettner Dec 16 '21 at 06:36
  • @ThorstenKettner Pensioner of 20 years old? well, the fact that **all** years are treated as 20th centure year is marked as bold, if this is not safe then according CASE (instead of single `'-19'`) must be used. The problem with quoted unit name fixed, thanks. – Akina Dec 16 '21 at 06:39
  • SELECT `PensionerDOB`, YEAR(CURDATE()) as Year, TIMESTAMPDIFF(YEAR, STR_TO_DATE(CONCAT(SUBSTRING_INDEX(PensionerDOB, '-', 2), '-19', SUBSTRING_INDEX(PensionerDOB, '-', -1)), '%d-%M-%Y'), CURRENT_DATE) AS age FROM pensionerbasicdata – Mukhila Asokan Dec 16 '21 at 06:43
  • @Asokan Asterisk means that you may/must place needed column names and/or expressions instead. – Akina Dec 16 '21 at 06:44
0

Try this one it will work,

Query,

SELECT DATE_FORMAT(FROM_DAYS(DATEDIFF(now(),YourDateofBirth)), '%Y')+0 AS Age from AgeCalculationFromDatetime
mightyteja
  • 825
  • 1
  • 14
  • 38
0

here Mysql is not parsing two-digit years as expected.. Instead of 1945- it's returning 2065,1953- it's returning 2053. Please follow this link to parse the date with two digits years. how-to-use-str-to-date-mysql-to-parse-two-digit-year-correctly

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 16 '21 at 06:58