Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreSQL get last value in a comma separated list of values

Tags:

sql

postgresql

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?

like image 933
Jayadevan Avatar asked Oct 23 '25 16:10

Jayadevan


1 Answers

I would do it this way:

select reverse(split_part(reverse(myColumn), ',', 1))
like image 126
Gordon Linoff Avatar answered Oct 25 '25 07:10

Gordon Linoff