6

Is it possible to trim a text array in PostgreSQL? If so, how?

I want to get something like:

    select trim(myTextArrayColumn) from myTable;

where myTextArrayColumn is of type text[].

Example column values:

    {"someData1 ", "someData2 "}

    {" someData3 "}
0

2 Answers 2

4

This should do it:

select array_agg(trim(e))
from (
  select row_number() over () as rn, 
         unnest(myTextArrayColumn) e
  from mytable
) t
group by rn
Sign up to request clarification or add additional context in comments.

1 Comment

Works smoothly! (I'm using postgres 13.x)
2

try this, i think suitable for your

select string_to_array(replace(array_to_string(arrColumn, '::'), ' ', ''), '::') from myTable

note: 1. all spaces will be removed 2. your values must does not contains '::', if contains use other delimeter

1 Comment

To avoid replacing all spaces, you can trim with this modification, select string_to_array(replace(replace(array_to_string(arrColumn, '::'), ' ::', '::'), ':: ', '::'), '::') from myTable

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.