5 pts.
 Concentrate more than two fields together into one field in SQL
Say I have 3 columns: 1st name, middle name and last name. How do I merge the 3 columns into one column yet separate the content with commas?

Software/Hardware used:
ASKED: June 13, 2008  2:55 PM
UPDATED: April 19, 2013  2:00 PM

Answer Wiki:
SQL Server doesn't have the STRIP and CONCATINATION functions which were mentioned in the prior version of the answer. You use the + sign to join the strings together, something like this.
<pre>SELECT FirstName + ', ' + CASE WHEN MiddleName IS NULL THEN '' ELSE MiddleName + ', ' END + LastName
FROM YourTable</pre>
I wrapped the MiddleName column within a CASE statement so that if it's NULL it leaves the middle name out. If you want the blank field if the MiddleName is NULL then you'll want something like this.
<pre>SELECT FirstName + ', ' + ISNULL(MiddleName) + ', ' + LastName
FROM YourTable</pre>
Tiny modification use an As to give column name-
<pre>SELECT (FirstName + ', ' + ISNULL(MiddleName) + ', ' + LastName) as FullName
FROM YourTable</pre>
Last Wiki Answer Submitted:  April 19, 2013  2:01 pm  by  Michael Tidmarsh   11,400 pts.
All Answer Wiki Contributors:  Michael Tidmarsh   11,400 pts. , Denny Cherry   64,520 pts. , DB2 DBA   40 pts.
To see all answers submitted to the Answer Wiki: View Answer History.


Discuss This Question:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _


 

Check out my SQL Server blog “SQL Server with Mr Denny” for more SQL Server information.

 64,520 pts.