Sql – What’s the shortest TSQL to concatenate a person’s name which may contain nulls

sqlsql servertsql

3 fields: FirstName, MiddleName, LastName

Any field can be null, but I don't want extra spaces. Format should be "First Middle Last", "First Last", "Last", etc.

Best Answer

    LTRIM(RTRIM(
    LTRIM(RTRIM(ISNULL(FirstName, ''))) + ' ' + 
    LTRIM(RTRIM(ISNULL(MiddleName, ''))) + ' ' + 
    LTRIM(ISNULL(LastName, ''))
    ))

NOTE: This won't leave trailing or leading spaces. That's why it's a little bit uglier than other solutions.

Related Topic