select firstname + ' '+ middlename +' '+lastname as person
from person.Person
and this will return nulls in rows where any of the columns have a null:
There are a couple of solutions you can consider to resolve this. One is to use the ISNULL function to let SQL Server know what to do with the nulls.
ISNULL
select firstname + ' '+ ISNULL(middlename,'')+' '+lastname as personfrom Person.Person
For the second argument I have used '' to return an empty string in place of the null:
I could have put any value I wanted in place of the null eg:
select firstname + ' '+ ISNULL(middlename,'NO MIDDLENAME')+' '+lastname as person
from Person.Person
will put the string NO MIDDLENAME in place of null:
You could use COALESCE to handle the nulls.
COALESCE
select FirstName + ' ' + coalesce(MiddleName,'') + ' ' + LastName as personfrom person.Person
Like ISNULL, COALESCE allows you to pass an argument in to handle the nulls.
Also like ISNULL, COALESCE will also allow you to replace the null with any value you choose.
No comments:
Post a Comment