Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

340
Visualizações
How to find the starting position of the last word from the string in PostgreSQL

What will be the Postgres equivalent for the below code from Oracle.

Select instr('abc de fgh',' ', -1) from dual;

returns: 7

Original code: substr(country, instr(country,' ', -1)+1);

I want to create the same logic in Postgres but position & reverse function didn't work

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

You apparently want the last element from the string where elements are delimited by a space character.

If you are using Postgres 14, you can use:

split_part('abc de fgh', ' ', -1);

which returns fgh

Or with a column reference: split_part(country, ' ', -1)

In earlier versions, split_part() doesn't allow negative offsets, so you would need something different:

(string_to_array('abc de fgh', ' '))[cardinality(string_to_array('abc de fgh', ' '))] 

It's a bit shorter with a column reference:

(string_to_array(country, ' '))[cardinality(string_to_array(country, ' '))] 

This first converts the string into an array, then picks the last element of the array (which in turn is determined using the cardinality() function that returns the length of the array)

over 4 years ago · Santiago Trujillo Relatório

0

Since you mention REVERSE, you can use:

SELECT RIGHT(value, POSITION(' ' in REVERSE(value)) - 1) AS country,
       LENGTH(value) - POSITION(' ' in REVERSE(value)) + 1 AS pos
FROM   (SELECT 'abc de fgh' AS value) v;

Which outputs:

country pos
fgh 7

db<>fiddle here

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda