Full Name – get rid of the empty space if middle name is null

by Yugolancer


Posted on Sunday, March 6, 2016



I see people bother with checking against NULL, using Coalesce, Substring etc. etc.

The solution is pretty simple actually. You just need to replace the two empty space by one if the middle name is null or empty e.g.

C#


FullName = (reader["FirstName"].ToString() + " " + reader["MiddleName"].ToString() + " " + reader["LastName"].ToString()).Replace("  ", " ");

T-SQL


REPLACE(FirstName + ' ' + MiddleName + ' ' + LastName + ' ' + Suffix, '  ', ' ') AS FullName


Copyright © ASPNETer 2006 - 2016