SQL - Concat full name, and a space only if last name is present
<p>I am currently concatenating a first and a last name like this (using PostgreSQL):</p>

<pre><code>concat(customers.firstname, ' ', customers.lastname)
</code></pre>

<p>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.</p>

<p>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.</p>

<p>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?</p>