13

Concatenate SQL Server Columns into a String with CONCAT()

 3 years ago
source link: https://www.mssqltips.com/sqlservertip/2985/concatenate-sql-server-columns-into-a-string-with-concat/?utm_campaign=Feed%3A+MSSQLTips-LatestSqlServerTips+%28MSSQLTips+-+Latest+SQL+Server+Tips%29
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Concatenate SQL Server Columns into a String with CONCAT()

By: Chad Churchwell   |   Updated: 2021-07-21   |   Comments (17)   |   Related: 1 | 2 | 3 | 4 | 5 | 6 | More > Functions - System

Problem

I need to produce mailing labels from my Microsoft SQL Server database so I am using the + sign to concatenate the first, middle, and last names together. The issue I see is I get NULL for a lot of rows. This makes me unable to produce the full names. What are some options to address this problem? Check out this tutorial to learn more about concatenating data in SQL Server with T-SQL string concatenation.

Solution

Prior to SQL Server 2012 concatenation was accomplished by using the plus (+) sign to concatenate fields together of various data types (varchar, char, int, numeric, etc.). The limitation of this method is if any of the fields you are concatenating are NULL, the final string value is NULL. In SQL Server 2012 and later there is the CONCAT() function that replaces NULL with an empty string. Take a look at this tip to see how this new function works and how it can be beneficial in your code.

For this demo I am going to use the Person.Person table from the AdventureWorks2012 database to demo the SQL functions to generate a full name for creating mailing labels. First, the following example is the old technique to concatenate strings using the + sign (concatenation operator):

SELECT 
    Title, 
    FirstName, 
    MiddleName, 
    LastName,
    Title + ' ' + FirstName + ' ' + MiddleName + ' ' + LastName as MailingName
FROM Person.Person

As you can see in the screen shot below the MailingName is NULL for any row that has NULL for any one of the name columns. The only rows that have MailingName filled in have a value for all the title, firstname, middlename, and lastname columns. This could be corrected by wrapping ISNULL(column,'') around all the columns in the concatenated field to account for any values having nulls, but that code gets long, messy, and hard to read.

Concat the old way

Below is example syntax is using ISNULL along with the plus sign to concatenate values. The ISNULL function will replace NULL values with the value noted in the second parameter, which in this example is an empty string.

SELECT 
    Title, 
    FirstName, 
    MiddleName, 
    LastName,
    ISNULL(Title,'') + ' ' + ISNULL(FirstName,'') + ' ' + ISNULL(MiddleName,'') + ' ' + ISNULL(LastName,'') as MailingName
FROM Person.Person

As you can see in the example below, the MailingName is no longer NULL as it replaced the NULL values with an empty string. This achieves the same as using the CONCAT() function, but requires a lot more code and readability.

Concat with ISNULL

The next set of code is using the new CONCAT() function that is in SQL Server 2012 and later versions with a SELECT statement. It replaces NULL values with an empty string of type VARCHAR(1). This SQL statement is much easier to read and write when you need to have NULL code handling in place and generate a single string in a single column with spaces as a separator.

SELECT 
    Title, 
    FirstName, 
    MiddleName, 
    LastName,
    CONCAT(Title,' ',FirstName,' ',MiddleName,' ',LastName) as MailingName
FROM Person.Person

If you see the results of this, all MailingName values are present, even if they have some of the columns set to NULL.

CONCAT the new way

As you can see this new function is very handy and behaves much different that the old form of concatenation. Instead of evaluating to NULL if any if the columns contain NULL values, the CONCAT() function replaces it with an empty string. This is very useful for coding around NULL values.

Next Steps
Last Updated: 2021-07-21
About the author

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK