Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Is there a "best practice" for concat'ing names?

Author  Topic 

toddhd
Yak Posting Veteran

55 Posts

Posted - 2010-11-04 : 16:41:46
I see this all the time online, and we have tons of examples in our own code at work...

SELECT (LastName + ', ' + FirstName) AS Name

This is really a horrible way to format a name, because if either LastName or FirstName (or both) are NULL then you get useless output.

Examples:
Doe, John
Doe,
, John
,


I can come up with several ways to handle this (some rather wordy) but I was wondering if there was a "best practice" way of handling this? If either or both fields are null or empty, I don't want the comma hanging out.

-Todd Davis

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-11-04 : 17:08:51
Maybe this:
select coalesce(LastName + ', ','')+coalesce(FirstName,'') as [Name]

edit: there was a space missing

No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page
   

- Advertisement -