Page 1 of 1

Find the last occurrence of a char in a string

Posted: Wed Apr 23, 2014 5:04 pm
by fsalvelt
How can I find the last occurrence of a character in a string? For instance, I want to find the position of the last "s" in the following string:

abcdfshakfdbfkdsksbdw

Re: Find the last occurrence of a char in a string

Posted: Wed Apr 23, 2014 8:43 pm
by JimKnicely
fsalvelt,

You can use a -1 as the starting position of the INSTR function so that it works backwards.

Example

Code: Select all

dbadmin=> select instr('abcdfshakfdbfkdsksbdw', 's', -1);
 instr
-------
    18
(1 row)