SQL Server String Concatenation with Null

后端 未结 10 807
隐瞒了意图╮
隐瞒了意图╮ 2020-11-27 13:51

I am creating a computed column across fields of which some are potentially null.

The problem is that if any of those fields is null, the entire computed column will

10条回答
  •  醉话见心
    2020-11-27 14:49

    You can also use CASE - my code below checks for both null values and empty strings, and adds a seperator only if there is a value to follow:

    SELECT OrganisationName, 
    'Address' = 
    CASE WHEN Addr1 IS NULL OR Addr1 = '' THEN '' ELSE Addr1 END + 
    CASE WHEN Addr2 IS NULL OR Addr2 = '' THEN '' ELSE ', ' + Addr2 END + 
    CASE WHEN Addr3 IS NULL OR Addr3 = '' THEN '' ELSE ', ' + Addr3 END + 
    CASE WHEN County IS NULL OR County = '' THEN '' ELSE ', ' + County END 
    FROM Organisations 
    

提交回复
热议问题