-1

Can anyone help me to find the alternative of age function in Bigquery. Below is some example of AGE function in netezza database. ex:

SELECT AGE(Current_date,'1994-07-16');

o/p: 27 years 2 mons 21 days

Thanks in advance.

Mikhail Berlyant
  • 165,386
  • 8
  • 154
  • 230

2 Answers2

0

Try this custom function. This function does not take care of leap years.

create function mydataset.age(_date date)
returns string  
as
(
    (
        select  concat(
            case when years = 0 then '' else  concat(cast(years as string),' Year ') end,
            case when months = 0 then '' else  concat(cast(months as string),' Month') end,
            case when days = 0 then '' else  cast(days as string) end,' Day'
            )
    from (
        select
        div(date_diff(current_date(),_date,day),365) years,
        div(mod(date_diff(current_date(),_date,day),365),30) months,
        mod(mod(date_diff(current_date(),_date,day),365),30) days
    )
    )
        
)
Mr.Batra
  • 787
  • 1
  • 5
  • 11
  • Hi Mr. Batra, Thank you for you approach. In the above query year is coming correctly but month and days are not comming correct wise. The output is coming as " 27 Year 3 Month Day" While it should come as "27 years 2 mons 22 days" For date '1994-07-16'. Trouble is with month and days. – Radha Raman Oct 07 '21 at 13:45
  • This function does not take care of leap years. You need to modify as per your need. – Mr.Batra Oct 08 '21 at 05:13
0

Consider below approach

create temp function age (dob date, base date) as ((
  select format('%i years %i months %i days', years, months, date_diff(base, temp_date2, day))
  from (
    select *,
      date_diff(base, temp_date, month) - day_check months,
      date_add(temp_date, interval date_diff(base, temp_date, month) - day_check month) temp_date2   
    from (
      select *,
        date_diff(base, dob, year) - month_check as years,
        date_add(dob, interval date_diff(base, dob, year) - month_check year) temp_date
      from (
        select 
          if(extract(month from base) < extract(month from dob), 1, 0) month_check,
          if(extract(day from base) < extract(day from dob), 1, 0) day_check
      )
    )
  )    
));
select age('1994-07-16', current_date) as age    

with output

enter image description here

Mikhail Berlyant
  • 165,386
  • 8
  • 154
  • 230
  • Hi Mikhail, Thanks for your approach. But by using this age in year is coming correct but months and days are not coming correct. In Example : SELECT AGE('1994-07-16'); o/p: 27 years 2 mons 22 days . This is the correct output i tried in netezza. But as per our above function it is coming as 27 years 7 months 15 days – Radha Raman Oct 07 '21 at 13:41
  • good catch, will investigate where the problem is – Mikhail Berlyant Oct 07 '21 at 14:47
  • see new version. i feel like there is a bug in recently introduced interval functions - so I had to rewrite my initial solution to not to use those. not heavily tested though. please try and let know if this works as expected – Mikhail Berlyant Oct 07 '21 at 17:41
  • Hi Mikhail, I got an issue while running the above new code, ex: age('1994-07-16', current_date) returns o/p: 27 years 2 months 25 days And – Radha Raman Oct 11 '21 at 06:13
  • so it looks correct to me ... ? – Mikhail Berlyant Oct 11 '21 at 06:16
  • Sorry. I am saying the issue. That comment was my mistake – Radha Raman Oct 11 '21 at 06:17
  • Hi , The above function is working very much simmilar per BQ. Thanks, But found the some below bugs: 1. age('1994-10-19', current_date) : 27 years -1 months 22 days Expected is : 26 years 11 mons 23 days as per netezza. For current months previous month it is coming as -1. – Radha Raman Oct 11 '21 at 06:54
  • And 2.age('1994-07-12', current_date) : 27 years 2 months 29 days As in Netezza : 27 years 2 months 30 days --> 30 days in NZ and 29 days in BQ. So we are getting one day less than Netezza age('1994-07-12', current_date) : 27 years 2 months 29 days As in Netezza : 27 years 2 months 30 days – Radha Raman Oct 11 '21 at 06:55
  • Hope you can adjust code if you need it to be exact as nz! – Mikhail Berlyant Oct 11 '21 at 06:59
  • Sure. Thanks... I will try – Radha Raman Oct 11 '21 at 07:07
  • I would rather trust calendar - double check correct age via calendar and if it is really just one day discrepancy - the fix is trivial – Mikhail Berlyant Oct 11 '21 at 07:28
  • @RadhaRaman - so, where you are with this? were you able to adjust code for that one day discrepancy? – Mikhail Berlyant Oct 20 '21 at 20:54
  • Yes @Mikhail . I am able to adjust the it. Thank you for your help :) – Radha Raman Oct 22 '21 at 06:50
  • Yes, it helped me alot. I accept – Radha Raman Oct 25 '21 at 05:01