8

I'm looking for a postgresql function that will do the opposite of string_agg.

I have a movies table where the tags column contains values such as

Action|Adventure|Drama|Horror|Sci-Fi
Action|Horror|Sci-Fi

I would like to get a distinct list of tags from this column, for example

Action
Adventure
Drama
Horror
Sci-Fi
Superdooperhero
  • 7,584
  • 19
  • 83
  • 138

2 Answers2

9

You can use unnest() and string_to_array():

 select unnest(string_to_array(t.col, ','))
 from t
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
4

You need string_to_array() combined with unnest()

select t.tag
from movies, unnest(string_to_array(tags,'|')) as t(tag)