Nina
Nina

Reputation: 1

Calculate the Age from the DOB

I am using HiveQL and I need to calculate the age just by using the Date of birth column but the issue is GetDate doesn’t work however Current_Date() does. The example I am trying is

datediff(yy,Dateofbirthcol,current_date()) As Age.

The DOB column looks like 1988-12-14.

Upvotes: 0

Views: 10602

Answers (2)

leftjoin
leftjoin

Reputation: 38335

Do not use unix_timestamp() because it is non-deterministic. Use current_date:

 datediff(current_date, Dateofbirthcol) / 365.25

Upvotes: 0

zealous
zealous

Reputation: 7503

Try one of the following option.

  1. floor(datediff(to_date(from_unixtime(unix_timestamp())), Dateofbirthcol) / 365.25)

  2. datediff(now(), Dateofbirthcol) / 365.25

Upvotes: 2

Related Questions