I am writing some SQL code where i have a comma separated string with 3 values. I would like to transform this string into an array and write it into the first 3 elements of a larger array.
My naive approach was this:
DO $$
DECLARE
my_string varchar := '1.0,1.1,1.2';
target_array real[6] := '{0., 0., 0., 0., 0., 0.}';
BEGIN
select string_to_array(my_string, ',') into target_array;
raise notice '%', target_array;
END$$;
this returns NOTICE: {1,1.1,1.2}
what i want is NOTICE: {1,1.1,1.2,0.,0.,0.}
i tried to slice the target array target_array[:3]
but this results in a syntax error.
So, how do i do this?
2
Answers
Concatenate
my_string
converted to an array (i.e.my_string_array
) with a slice oftarget_array
that has its first elements removed.Or you can use a loop
Both produce
{1,1.1,1.2,0,0,0}
.See if this solves your problem :