Birthdays and Ages

 How do I calculate current ages? I am trying to create a list of active patients 18 and younger with their ages and birthdays.

You might think that you could use the standard SQL function called DATEDIFF to just subtract the years, but that won’t give you an accurate age if the current date is earlier in the year than the birthday. To get a true age calculation, use the custom function “AGEINYEARS” that SOS has added to your database. It takes two parameters, the date of birth and the reference date on which you want to calculate age. Most of the time that will be CURRENT DATE, but you can use a different date if desired.

SELECT
  lastname  AS "Last Name",
  firstname AS "First Name",
  id,
  sos.AGEINYEARS(dob,CURRENT DATE) AS "Current Age" ,
  DATEFORMAT (dob, 'MM-DD-YYYY') AS "Date of Birth"
FROM
  sos.patients
WHERE
  flag = 0  /* active patients only */
  AND dischargedate IS NULL    /* no discharge date has been entered */
  AND "Current Age" <= 18  /* only patients younger than 18 years of age */
ORDER BY 
  lastname,firstname, id

 

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

This site uses Akismet to reduce spam. Learn how your comment data is processed.