Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find sub string position from the end of string in PostgreSQL

Tags:

sql

postgresql

I have this query which finds the position of sub string.

select position('-' || lower('i') in lower('GFT-iMB5-i'))

(this is an example it actually uses function variables to replace the strings)

This return 4

In a nut shell I want it to begin from the end of the string and not from the start. meaning I want to get: 9

How can I do that?

like image 263
avi Avatar asked Oct 15 '25 18:10

avi


1 Answers

One method is to reverse the values and do comparison that way:

select length('GFT-iMB5-i') - position(reverse('-' || lower('i')) in reverse(lower('GFT-iMB5-i')))
like image 103
Gordon Linoff Avatar answered Oct 18 '25 07:10

Gordon Linoff