0

I have a PostgreSQL table, one column of type text contains data like '{123456,345678}'. Is there a way in PostgreSQL to convert this to an array?

6
  • A convoluted stored procedure parser? If you're brave you can use PLV8 which gives you a whole JavaScript runtime, where parsing this is trivial. Commented Aug 10, 2022 at 19:44
  • So source is Postgres and destination is also Postgres, but an array of ints/text? Are you doing ad-hoc conversions (per query) or creating a new column and persisting converted data? Either way, there is a way to create a new array from text as long as you trust the text is in the right format. But if you have some values that are array-like and some that are not, that will be harder to implement. Commented Aug 10, 2022 at 19:48
  • Check out string functions. Specifically, string_to_array (to split at comma) coupled with replace (to remove braces). Commented Aug 10, 2022 at 19:52
  • 5
    You can cast it: '{123456,345678}'::int[] Commented Aug 10, 2022 at 20:00
  • 1
    It already looks like an array of integers, this works fine: SELECT CAST('{123456,345678}' AS INTEGER[]); Commented Aug 10, 2022 at 20:56

0

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.