T-SQL CASE Clause: How to specify WHEN NULL

后端 未结 15 1963
心在旅途
心在旅途 2020-11-28 05:42

I wrote a T-SQL Statement similar like this (the original one looks different but I want to give an easy example here):

SELECT first_name + 
    CASE last_na         


        
15条回答
  •  萌比男神i
    2020-11-28 06:02

    The WHEN part is compared with ==, but you can't really compare with NULL. Try

    CASE WHEN last_name is NULL  THEN ... ELSE .. END
    

    instead or COALESCE:

    COALESCE(' '+last_name,'')
    

    (' '+last_name is NULL when last_name is NULL, so it should return '' in that case)

提交回复
热议问题