patternsqlModerate
Get list of characters in column
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:
The resulting list would be
The order is not important but could be alphabetical
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 | BobThe resulting list would be
petrandboThe 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
) tThe 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
) tContext
StackExchange Database Administrators Q#83676, answer score: 16
Revisions (0)
No revisions yet.