COALESCE function - Mailing list pgsql-general

From Kevin Hunter
Subject COALESCE function
Date
Msg-id 45974E36.1030108@earlham.edu
Whole thread Raw
Responses Re: COALESCE function  (mike <mike@thegodshalls.com>)
List pgsql-general
Hello All,

Attempting to select two different column types with COALESCE returns
this error:

ERROR:  COALESCE types smallint and character varying cannot be matched

Attempting the same thing with a CASE statement returns a similar error:

ERROR:  CASE types smallint and character varying cannot be matched

I also checked Oracle's NVL command, and it throws a similar error.
Clearly, I'm not supposed to intermix two different column types into a
SELECT statement.  This is because the engine needs to return a set
given the criteria, and it's difficult to do that with criteria that
/depends on the data/, yes?

Could someone explain a/the more formal reason why I can't do what I'm
trying to do?

The relevant part of my SELECT statement:

SELECT
   ...,
   COALESCE(f.number, f.name),
   ...
FROM
   ...,
   field AS f,
   ...
WHERE
    ...
;

f.number ∈ SMALLINT
f.name   ∈ CHARACTER VARYING

Thank you in advance!

Kevin

P.S. If something gets lost in bit/encoding translation, ∈ = "Element Of"


pgsql-general by date:

Previous
From: David Fetter
Date:
Subject: Re: Generic timestamp function for updates where field names vary
Next
From: mike
Date:
Subject: Re: COALESCE function