Requires Free Membership to View
Use the COALESCE function. If LName will never be NULL, you'd need to use it only on the Fname column.
select LName
+ coalesce( ', ' + Fname, '' )
as Membname
from yourtable
Note here how Fname is first concatenated to a string consisting of a comma followed by a space. Now since NULLs propagate through expressions, therefore if Fname is NULL, then the concatenation of ', ' and Fname will also be NULL, and thus, the COALESCE function will return a zero-length or empty string. Otherwise, the concatenation of ', ' and Fname will be concatenated to Lname.
The net effect is that Membname will be either Lname only, or Lname followed by a comma and space and then Fname. Sweet, eh?
This was first published in March 2005
Join the conversationComment
Share
Comments
Results
Contribute to the conversation