
When you're creating an age-sensitive application such as a hospital or an insurance company application, you'll encounter situations where you need to compute clients' age based on their date of birth. While it might be easier to store the patients' age in a database table and retrieve those values without further computations, it is not the most optimal solution since the age of patients change with time. A good approach is to store the date of birth in a database table and use the MySQL built-in functions to compute the age dynamically. The company employees can then use the computed values to make better decisions. For instance, since the ability to metabolize medicine changes with age, reporting the patient's exact age to a doctor can help them choose the appropriate dose. In an insurance company, the client's age is one of the variables used to calculate the risk and pricing of the premiums.
In this guide, you'll use the MySQL TIMESTAMPDIFF function to accurately calculate clients' age based on a date of birth (dob) column on Ubuntu 20.04.
To complete this tutorial, make sure you've all the following:
test_db DatabaseSSH to your server and log in to MySQL as root.
Enter your root password for the MySQL server and hit Enter to continue. Next, issue the command below to set up a test_db database.
Select the test_db database.
Create a patients table. In this table, you'll store patients' bio-data, including their full names, phone, and date of birth. To uniquely identify each patient, use the patient_id column as the PRIMARY KEY.
Once the table is in place, populate it with some records.
Make sure you get the confirmation below after running each INSERT statement.
In MySQL, you can use the inbuilt TIMESTAMPDIFF() function to compute the difference between two dates and return a value either in days, months or years.
See the basic syntax below.
To retrieve the ages of patients from your patients table, pass the dob as the FIRST_DATE and the current date(CURDATE())) as the SECOND_DATE as shown below.
Your full MySQL query for retrieving patients' age in years alongside the other bio-data columns should be similar to the syntax below.
Once you run the query, you should get the output shown below.
While the MySQL query above looks great, it displays 0 output for patients with less than one year(BABY ROE). This is because the TIMESTAMPDIFF only returns the number of complete periods (in this case, years) between two dates while taking into account the varying days in each month(28, 29, 30, 31) and leap years (either 365 or 366 days).
The output might mislead health and insurance records because clients younger than one year old are reported as having 0 years.
To overcome this challenge, use a combination of MySQL functions to compute the exact age. Run the query below.
In the syntax above, you're using the MySQL TIMESTAMPDIFF function to compute the number of complete months between the patients' date of birth (dob) and the current date (CURDATE()). Then, you're using the MySQL FLOOR function to get only the full months without any decimals. To retrieve the years, you're dividing the months by 12.
Next, you're using the MySQL MOD function to retrieve the remaining months that don't add up to one year. Then, you're using the MySQL CONCAT statement to combine the resulting value into one string.
After you run the above query, you should now get the patients' age showing their exact age in years and months.
Use the computed age above when displaying the clients' bio-data in your frontend mobile or desktop application to allow end-user to make valid decisions based on the computed age.
In this guide, you've set up a sample database and used the MySQL built-in functions including TIMESTAMPDIFF, CONCAT, FLOOR, and MOD to dynamically compute the ages of patients based on their date of birth.
0 Comments
Be the first to comment and share your perspective with the community.