此SQL似乎很复杂,当一个或两个字段可以为NULL时,是否有更简单的方法来获取FirstName,LastName?
SELECT COALESCE(LastName,'')+ CASE WHEN LastName+FirstName IS NOT NULL THEN ', ' END+ COALESCE(FirstName,'') AS Name FROM Person
怎么样
SELECT COALESCE(LastName + ', ' + FirstName, LastName, FirstName) Name FROM Person
如果firstname或lastname为null,则整个第一个表达式(带有,)将变为null,迫使合并检查第二个lastname单独的表达式,然后如果lastname为null则最后一个firstname单独的表达式。
firstname
lastname
,