How to get an age from a D.O.B field in MySQL?

后端 未结 13 1540
鱼传尺愫
鱼传尺愫 2020-11-29 07:01

I need to calculate the age of a \"customer\" from their date of birth.

I have tried to use the following:

DATEDIFF(year, customer.dob, \"2010

相关标签:
13条回答
  • 2020-11-29 07:45

    If you want years, months and days like "26 years 4 months and 27 days" then following is the age function for you:

    delimiter $$
    
    create function age(dob date)
    returns varchar(30)
    deterministic
    begin
    declare years int default 0;
    declare months int default 0;
    declare days int default 0;
    declare today date default curdate();
    
    set years = timestampdiff(year, dob, today);    
    set months = timestampdiff(month, dob, today - interval years year);    
    set days = timestampdiff(day, dob, today - interval years year - interval months month);
    
    return (concat(years, ' years ', months,' months and ', days,' days'));
    
    end$$
    
    delimiter;
    

    We are calculating the age from current date so I set today to curdate(). Now we are getting our years, months and days as following.

    1. years: We get the difference in years from today to date of birth.

    2. months: First we subtract the years from the today so that we only get months that are extra from the years and then find the difference in months from date of birth. Here "years" is the variable and "year" is interval type.

    3. days: And here we subtract years and months that we have already calculated from today which is current date then find the diff in days from birth. Here "months" is the variable and "month" is interval type.

    You can use datetime instead of date as argument if you want also you can format the return values as you like.

    0 讨论(0)
  • 2020-11-29 07:45
    DELIMITER $$ DROP FUNCTION IF EXISTS `test`.`__AGE` $$
        CREATE FUNCTION `_AGE`(in_dob datetime) RETURNS VARCHAR(100)
            NO SQL
        BEGIN
           DECLARE l_age VARCHAR(100);
           DECLARE YEARS INT(11);
           DECLARE MONTHS INT(11);
           DECLARE DAYS INT(11);
           DECLARE DIFFS FLOAT;
    
    
           SET DIFFS=DATEDIFF(CURRENT_DATE(),in_dob) /365.25;
           SET YEARS=FLOOR(DIFFS) ;
           SET MONTHS=FLOOR((DIFFS - YEARS)*365.25/30.4375) MOD 12;
           SET DIFFS=((DIFFS - YEARS)*365.25/30.4375);
           SET DAYS=CEIL(((DIFFS-MONTHS)*30.4375)) MOD 31;
           RETURN(CONCAT(YEARS, " Year ",MONTHS," Month ",DAYS," Days"));
    
        END $$
    DELIMITER ;
    
    SELECT __Age(CAST('1980-07-16' AS DATE));
    
    0 讨论(0)
  • 2020-11-29 07:46

    This is the simplest I could come up with so far:

    SELECT FLOOR(ABS(DATEDIFF(d, CURRENT_TIMESTAMP, dob))/365.25) AS age
    

    First we get the date difference in days, then convert it to years, then FLOOR truncates to the integer part of the number.

    0 讨论(0)
  • 2020-11-29 07:48
    DELIMITER $$ DROP FUNCTION IF EXISTS `test`.`_AGE` $$
        CREATE FUNCTION `_AGE`(in_dob datetime) RETURNS VARCHAR(100)
            NO SQL
        BEGIN
           DECLARE l_age VARCHAR(100);
           DECLARE YEARS INT(11);
           DECLARE MONTHS INT(11);
           DECLARE DAYS INT(11);
           DECLARE DIFFS FLOAT;
    
    
           SET DIFFS=DATEDIFF(CURRENT_DATE(),in_dob) /365.25;
           SET YEARS=FLOOR(DIFFS) ;
           SET MONTHS=FLOOR((DIFFS - YEARS)*365.25/30.4375) MOD 12;
           SET DIFFS=((DIFFS - YEARS)*365.25/30.4375);
           SET DAYS=CEIL(((DIFFS-MONTHS)*30.4375)) MOD 31;
           SET l_age=CONCAT(YEARS, " Year ",MONTHS," Month ",DAYS," Days");
           RETURN(l_age);
        END $$
    DELIMITER ;
    
    SELECT _Age(CAST('1980-07-16' AS DATE));
    
    0 讨论(0)
  • 2020-11-29 07:51
    SELECT DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(dob, '%Y') - (DATE_FORMAT(NOW(), '00-%m-%d') < DATE_FORMAT(dob, '00-%m-%d')) AS age
    
    0 讨论(0)
  • 2020-11-29 07:57

    Use Mysql recommended :

    TIMESTAMPDIFF(YEAR, dob, CURDATE()) AS age;
    

    Usage in a query :

    SELECT name, dob, TIMESTAMPDIFF(YEAR, dob, CURDATE()) AS age FROM pet;
    

    Ref: http://dev.mysql.com/doc/refman/5.0/en/date-calculations.html

    0 讨论(0)
提交回复
热议问题