How Old Am I (In Months)?

Jim_KnicelyJim_Knicely - Select Field - Administrator

You are probably aware that Vertica has a built-in function named AGE_IN_YEARS which returns the difference in years between two dates, expressed as an integer.

Example:

My son is turning 18 this year!

dbadmin=> SELECT age_in_years('06/13/2001');
age_in_years
--------------
           17
(1 row)

I was curious how many months he’s been on the planet so I figured that I could just multiply his current age by 12.

dbadmin=> SELECT age_in_years('06/13/2001') * 12 age_in_months;
age_in_months
---------------
           204
(1 row)

But that’s not completely accurate as it does not take into account that we are already 4 months into the current year and for any past Leap Years. Luckily there is a built-in Vertica function named AGE_IN_MONTHS that does!

dbadmin=> SELECT age_in_months('06/13/2001') actual_age_in_months;
actual_age_in_months
----------------------
                  214
(1 row)

Helpful Links:
https://www.vertica.com/docs/9.2.x/HTML/Content/Authoring/SQLReferenceManual/Functions/Date-Time/AGE_IN_MONTHS.htm
https://forum.vertica.com/discussion/239587/how-old-am-i

Have fun!

Sign In or Register to comment.