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,'')+
CASE WHEN LastName+FirstName IS NOT NULL THEN ', ' END+
COALESCE(FirstName,'') AS Name
FROM Person
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With