HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlModerate

Get list of characters in column

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
characterslistgetcolumn

Problem

I have a database table (UTF-8) with a lot of names in different languages with lots of special characters. In another script I use these names and one part is to replace the special characters. To know all appearing characters I need to get a list of them from somewhere.

Is it possible to get a distinct list of characters appearing in a column? What would be the way in PostgreSQL?

For example I have a table with names:

id | name
-----------
1  | Peter
2  | Andrea
3  | Bob


The resulting list would be

petrandbo


The order is not important but could be alphabetical

Solution

select string_agg(c,'')
from (
  select distinct regexp_split_to_table(lower(name),'') as c
  from data
) t


The inner select generates one row for each character, and the outer then aggregates that to a long string.

If you want the characters sorted, you can use an order by for the aggregate string_agg(c,'' order by c)

Code Snippets

select string_agg(c,'')
from (
  select distinct regexp_split_to_table(lower(name),'') as c
  from data
) t

Context

StackExchange Database Administrators Q#83676, answer score: 16

Revisions (0)

No revisions yet.