T-SQL CASE Clause: How to specify WHEN NULL

后端 未结 15 1907
心在旅途
心在旅途 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条回答
  • 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)

    0 讨论(0)
  • 2020-11-28 06:05

    Found a solution to this. Just ISNULL the CASE statement:

    ISNULL(CASE x WHEN x THEN x ELSE x END, '') AS 'BLAH'
    
    0 讨论(0)
  • 2020-11-28 06:05

    Use the CONCAT function available in SQL Server 2012 onward.

    SELECT CONCAT([FirstName], ' , ' , [LastName]) FROM YOURTABLE
    
    0 讨论(0)
提交回复
热议问题