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

161
Visualizações
Selecting non null values from columns of different types in postgres

I have a property values table as below and I know that only one of the *_value fields will contain a value, the others will be NULL.

What is the most efficient way to select the value of the column only that is not null?

I have tried

SELECT property_id, COALESCE(int_value, str_value, etc)

but this doesn't work because they are different column types, similarly with NULLIF. I can cast all the values but I don't want to lose the value types.

property_values
-----------------------------------------------
property_id      | integer
int_value        | integer
str_value        | character varying
time_value       | timestamp without time zone
txt_value        | text
dec_value        | numeric
bool_value       | boolean
json_value       | jsonb
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

This is too long for a comment.

You cannot do what you want, at least "reasonably". As you note, coalesce() doesn't work because of the type conflict. That gets close to the root of the problem: a column in a result set has to have a specific type.

So, what type do you want the result to be? You could decide on a "universal" type, such as a string. I'm not sure that is such a good idea for your use case.

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