【问题标题】:COALESCE types character varying and numeric cannot be matchedCOALESCE 类型字符变化和数字无法匹配
【发布时间】:2020-06-02 17:20:13
【问题描述】:

我有一个名为 table1 的表,其中包含 Gender varchar(10) 和 Team numeric 列。

create table table1 (
ID integer
Gender varchar(10),
Team numeric
);

insert into table1 (ID,Gender,Team) values
(1,'M',NULL),
(2,NULL,10),
(3,NULL,6),
(4,''F',NULL),
(5,NULL,3);

我想创建一个新列作为Nxt,它从字符串或整数的任何列中返回一个不为空的行。

Nxt 列将如下所示:M,10,6,F,3

我试过了:

select coalesce(Gender,Team) as value from table1;

它返回此错误:

COALESCE 类型的字符变化和数字无法匹配

【问题讨论】:

  • 在传递给coalesce()之前将两者都转换为字符串。

标签: postgresql coalesce


【解决方案1】:

尝试将列转换为文本

    select coalesce(Gender,Team::text) as value from table1;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-01
    • 2017-11-03
    • 2013-04-17
    • 1970-01-01
    • 1970-01-01
    • 2014-03-19
    相关资源
    最近更新 更多