Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

160
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!