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

238
Visualizações
Convert digits extracted by substring match to Years in PostgreSQL 11.0

I have following table in PostgreSQL 11.0

min age      max age
1 Month      12 Months
1 Year       16 Years
1 Day        365 Days
365 Days     369 Days
N/A          N/A
NULL         NULL

I would like to convert the values to year. I am extracting the string after the digit and check if it is 'years', 'months' or 'days' and then convert the digit before the string to year.

I tried following query:

update tbl
set min_age = 
        case 
                when substring(min_age, '^\d+\s(.*)') ~* 'Month'
                then abs(substring(min_age, '^(\d+)\s.*')/12)
                when substring(min_age, '^\d+\s(.*)') ~* 'Months'
                then abs(substring(min_age, '^(\d+)\s.*')/12)
                when substring(min_age, '^\d+\s(.*)') ~* 'Day'
                then abs(substring(min_age, '^(\d+)\s.*')/365)
                when substring(min_age, '^\d+\s(.*)') ~* 'Days'
                then abs(substring(min_age, '^(\d+)\s.*')/365)
                when substring(min_age, '^\d+\s(.*)') ~* 'Year'
                then abs(substring(min_age, '^(\d+)\s.*'))
                when substring(min_age, '^\d+\s(.*)') ~* 'Years'
                then abs(substring(min_age, '^(\d+)\s.*'))
        end ;

update tbl
set max_age = case
                when substring(min_age, '^\d+\s(.*)') ~* 'Month'
                then abs(substring(min_age, '^(\d+)\s.*')/12)
                when substring(min_age, '^\d+\s(.*)') ~* 'Months'
                then abs(substring(min_age, '^(\d+)\s.*')/12)
                when substring(min_age, '^\d+\s(.*)') ~* 'Day'
                then abs(substring(min_age, '^(\d+)\s.*')/365)
                when substring(min_age, '^\d+\s(.*)') ~* 'Days'
                then abs(substring(min_age, '^(\d+)\s.*')/365)
                when substring(min_age, '^\d+\s(.*)') ~* 'Year'
                then abs(substring(min_age, '^(\d+)\s.*'))
                when substring(min_age, '^\d+\s(.*)') ~* 'Years'
                then abs(substring(min_age, '^(\d+)\s.*'))
           end

Expected output is:

min age       max age
0             1
1             16
0             1
1             1
N/A           N/A
NULL          NULL

Any help is highly appreciated.

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

0

Postgres has powerful interval functions. I think that a cast and justify_interval() might just work here:

select
    extract(year from justify_interval(min_age::interval)) min_age,   
    extract(year from justify_interval(max_age::interval)) max_age
from tbl

Demo on DB Fiddle:

min_yr | max_yr
:----- | :-----
0      | 1     
1      | 16    
0      | 1     
1      | 1     
over 4 years ago · Santiago Trujillo Relatório

0

This is how you can get all the values you need:

select split_part(min_age, " ", 1) as min_age_value, split_part(min_age, " ", 2) as min_age_unit, split_part(max_age, " ", 1) as max_age_value, split_part(max_age, " ", 2) as max_age_unit
from tbl;

Now, you can use the above as a substring and use case-when for min_age_unit and max_age_unit in the outer query, evaluating it to 1, 12 or 365 respectively and multiplying it with the actual value.

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