Question: I am currently concatenating a first and a last name like this (using PostgreSQL):

```
concat(customers.firstname, ' ', customers.lastname)

```

The problem I am having here is that I have customers that were entered with just a first name (ie. "Ronaldo"). The query above will return "Ronaldo " and that will not match.

I can use my server-side language to check for trailing spaces, but if there is a customer somewhere in my database that was accidentally typed in with a trailing space (ie. "Harry Potter "), then I won't match that one either.

I am looking for SQL that will return "Ronaldo", "John Smith", and "Harry Potter ". So basically I want to include a space between first and last name if last name is not equal to "" (empty string). If lastname is "" (empty string) then I want no space following the firstname. Is this possible with just SQL? Or will I have to figure out how to parse this appropriately with a server-side language?

Answer: 
I use this:

```
trim(both from COALESCE(firstname, '') || ' ' || COALESCE(lastname, '')

```

if both first and last name can be null.
This worked for me:

```
case
  when customers.lastname = ''
    then customers.firstname
  else
    concat(customers.firstname, ' ', customers.lastname)
end

```

Documentation for case: <https://www.postgresql.org/docs/7.4/static/functions-conditional.html>.

Special thanks to [daf](https://stackoverflow.com/users/1697008/daf).