I am trying to truncate value in an array of integer to 8 decimals in Postgres in a SQL SELECT statement and get back the cleaned array.
[1.030411088488374,1.0985247734044379,1.0863039797613594]
I am trying to truncate value in an array of integer to 8 decimals in Postgres in a SQL SELECT statement and get back the cleaned array.
[1.030411088488374,1.0985247734044379,1.0863039797613594]
You need to unnest the array, truncate every element, then aggregate the elements back into an array:
select array_agg(trunc(n, 8) order by idx)
from unnest(array[1.030411088488374,1.0985247734044379,1.0863039797613594]::numeric[]) with ordinality as t(n,idx);
If you need to do that a log, consider creating a function:
create function trunc_elements(p_numbers numeric[], p_num_digits int)
returns numeric[]
as
$$
select array_agg(trunc(n, p_num_digits) order by idx)
from unnest(p_numbers) with ordinality as t(n,idx);
$$
language sql;