Find the last occurrence of a char in a string

Moderator: NorbertKrupa

Post Reply
User avatar
fsalvelt
Intermediate
Intermediate
Posts: 54
Joined: Sun Mar 18, 2012 1:34 am

Find the last occurrence of a char in a string

Post by fsalvelt » Wed Apr 23, 2014 5:04 pm

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
Thank, Fred

User avatar
JimKnicely
Site Admin
Site Admin
Posts: 1825
Joined: Sat Jan 21, 2012 4:58 am
Contact:

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

Post by JimKnicely » Wed Apr 23, 2014 8:43 pm

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)
Jim Knicely

Image

Note: I work for Vertica. My views, opinions, and thoughts expressed here do not represent those of my employer.

Post Reply

Return to “New to Vertica SQL”