java java - 4 years ago 141
SQL Question

How to get last element in postgresql Text?

I have a text of elements seperated by

,
as follows:

'{4,56,7,3,2}'


the amount of elemets is unkonwn.

How do I get the last element?
in the above example
2

Answer Source

Try this:

SELECT regexp_replace('4,56,7,3,2', '^.*,', '')

FIDDLE DEMO

EDIT:

Try

SELECT replace(replace(regexp_replace('4,56,7,3,2', '^.*,', ''),'{',''),'}','')

DEMO

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download