【问题标题】:How do I replace NULL values with string using COALESCE function?如何使用 COALESCE 函数将 NULL 值替换为字符串?
【发布时间】:2022-07-06 00:46:59
【问题描述】:

尝试使用 select 语句中的 coalesce 函数将“未分配”设置为空值。 代码如下:

SELECT ORDER_ID, ORDER_DATE,
coalesce (SALES_REP_ID,'NA') REP
FROM ORDERS```
[But having error "inconsistent datatype"]
How do I fix it?

[Data, in which sales_rep_id have null values which i want to change it to 'unassigned']

【问题讨论】:

标签: sql oracle-sqldeveloper


【解决方案1】:

您需要确保不混合数据类型

SELECT ORDER_ID, ORDER_DATE,
       case when SALES_REP_ID is null 
            then 'NA'
            else to_char(SALES_REP_ID)
       end REP
FROM ORDERS

【讨论】:

  • 是否可以使用合并,因为我试图这样做并想以这种方式修复。这也是你现在表现的正确方式。
猜你喜欢
  • 2021-04-04
  • 2021-10-30
  • 2016-08-22
  • 2022-10-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多