FirstName, LastName in SQL, too complex?

前端 未结 3 1431
無奈伤痛
無奈伤痛 2021-01-01 05:07

This SQL seems complex, is there an easier way to get FirstName, LastName when one or both of the fields can be NULL?

SELECT COALESCE(LastName,\'\')+
                


        
3条回答
  •  没有蜡笔的小新
    2021-01-01 05:45

    How about

    SELECT COALESCE(LastName + ', ' + FirstName, 
                    LastName, FirstName) Name
    FROM Person
    

    if firstname or lastname is null the entire first expression (with the ,), becomes null, forcing the coalesce to examine, second, the lastname alone, and then if lastname is null, finally, the firstname alone.

提交回复
热议问题