【问题标题】:SQL Coalesce with empty stringSQL合并与空字符串
【发布时间】:2020-04-21 08:17:08
【问题描述】:

我有以下几点:

Select Coalesce(Other,Industry) Ind from registration

问题是Other 可以是空字符串或NULL。 如何让coalesce 工作,如果Other 是一个空字符串,Coalesce 仍然返回Industry

【问题讨论】:

  • 认为coalesce应该如何表现,因为它似乎不是标准的东西?

标签: sql sql-server


【解决方案1】:

使用CASE 表达式或NULLIF

SELECT COALESCE(NULLIF(Other,''),Industry) Ind FROM registration

【讨论】:

    【解决方案2】:

    试试这个

    Select Coalesce(nullif(Other,''),Industry) Ind from registration
    

    【讨论】:

    • 这是一个巧合,您的回答与接受的答案的时间完全相同。可惜你的评分选择了另一个。
    【解决方案3】:

    知道NULL <> '' 不会计算为TRUE,您也可以使用快捷方式...

    CASE WHEN other <> '' THEN other ELSE industry END
    

    然后逻辑如下...

    • CASE WHEN 'fubar' &lt;&gt; '' THEN other ELSE industry END
      => CASE WHEN true THEN other ELSE industry END
      => other

    • CASE WHEN '' &lt;&gt; '' THEN other ELSE industry END
      => CASE WHEN false THEN other ELSE industry END
      => industry

    • CASE WHEN NULL &lt;&gt; '' THEN other ELSE industry END
      => CASE WHEN NULL THEN other ELSE industry END
      => industry

    【讨论】:

      猜你喜欢
      • 2010-09-29
      • 2011-01-26
      • 2010-11-26
      • 2015-01-23
      • 1970-01-01
      • 2021-09-13
      • 2011-06-19
      相关资源
      最近更新 更多