How to concatenate columns properly using T-SQL?

前端 未结 6 1958
时光说笑
时光说笑 2020-12-05 14:11

I have a address in more than one column in a table.

SELECT FirstName, LastName, StreetAddress, City, Country, PostalCode 
FROM Client

I a

6条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-05 14:52

    Another solution is to use ISNULL

    Select FirstName, LastName
        , ISNULL(StreetAddress+', ','')
          +ISNULL(City+', ','')
          +ISNULL(Country+', ','')
          +ISNULL(PostalCode,'')
    FROM Client
    

    If a value is null, then the concatenation result will be null. ISNULL will replace the first expression with the second expression.

    http://msdn.microsoft.com/en-us/library/ms184325(v=SQL.90).aspx

提交回复
热议问题