SQL Coalesce with empty string

前端 未结 3 412
半阙折子戏
半阙折子戏 2020-12-09 00:57

I have the following:

Select Coalesce(Other,Industry) Ind from registration

The thing is that Other can be an empty string or

相关标签:
3条回答
  • 2020-12-09 01:05

    Use a CASE expression or NULLIF:

    SELECT COALESCE(NULLIF(Other,''),Industry) Ind FROM registration
    
    0 讨论(0)
  • 2020-12-09 01:10

    You can also use a short-cut knowing that NULL <> '' doesn't evaluate to TRUE...

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

    The logic then works out as follows...

    • CASE WHEN 'fubar' <> '' THEN other ELSE industry END
      => CASE WHEN true THEN other ELSE industry END
      => other

    • CASE WHEN '' <> '' THEN other ELSE industry END
      => CASE WHEN false THEN other ELSE industry END
      => industry

    • CASE WHEN NULL <> '' THEN other ELSE industry END
      => CASE WHEN NULL THEN other ELSE industry END
      => industry

    0 讨论(0)
  • 2020-12-09 01:25

    try this

    Select Coalesce(nullif(Other,''),Industry) Ind from registration
    
    0 讨论(0)
提交回复
热议问题