Say I have a varchar column let’s say religions
that looks like this: ["Christianity", "Buddhism", "Judaism"]
(yes it has a bracket in the string) and I want the string (not array) split into multiple rows like "Christianity", "Buddhism", "Judaism"
so it can be used in a WHERE
clause.
Eventually I want to use the results of the query in a where clause like this:
SELECT ...
FROM religions
WHERE name in
(
<this subquery>
)
How can one do this?
2
Answers
You can do the following.
split_part
function available in redshift, you can split the values based on the numbers generated in the temporary table by doing a cross join.regexp_replace
function in Redshift.You can use the function JSON_PARSE to convert the varchar string into an array. Then you can use the strategy described in Convert varchar array to rows in redshift – Stack Overflow to convert the array to separate rows.