PostgreSQL get last value in a comma separated list of values
postgresql, sql
Solution
With `regexp_replace`:
select id, regexp_replace(mycolumn, '.*,', '')
from mytable
order by id;
Problem
In a PostgreSQL table I have a column which has values like ``` AX,B,C A,BD X,Y J,K,L,M,N ``` In short , it will have a few comma separated strings in the column for each record. I wanted to get the last one in each record. I ended up with this. ``` select id, reverse(substr(reverse(mycolumn),1,position(',' in reverse(mycolumn)))) from mytable order by id ; ``` Is there an easier way?