COALESCE() for blank (but not null) fields

后端 未结 3 1423
你的背包
你的背包 2021-01-01 17:57

I have two fields that I\'m comparing with MySQL\'s function COALESCE(). For example, COALESCE(Field1, Field2). The problem is, Field1 is sometimes blank but no

相关标签:
3条回答
  • 2021-01-01 17:59
    SELECT IFNULL(NULLIF(Field1,''),Field2)
    

    NULLIF returns a NULL if Field1 is blank, while IFNULL returns Field1 if it's not blank or NULL and Field2 otherwise.

    0 讨论(0)
  • 2021-01-01 18:09

    I know I'm late to the party here, but there is a way to do this while still using COALESCE(). This would then work if your value was NULL or ''.

    Select COALESCE(NULLIF(Field1,''), Field2)
    
    0 讨论(0)
  • 2021-01-01 18:14

    You can use a CASE expression:

    CASE WHEN Field1 <> '' THEN Field1 ELSE Field2 END
    
    0 讨论(0)
提交回复
热议问题