This SQL tutorial focuses on SQL String Concatenation in SQL Server. For this lesson’s exercises, use this link.

This tutorial is a part of several posts explaining how to write basic queries in SQL Server. To read additional posts regarding this subject, please use the following links:


SQL Server Basic String Concatenation

SQL Server String concatenation allows you to append one string to the end of another string. To display the contents of two columns or more under the name of a single column, you can use the concatenation operator (+).
For example, to display the employee’s first name along with his or her last name, use the following SQL Server statement:

SELECT first_name + last_name
FROM employees

The result :

(No column name)
------------------------
JohnSmith

 

SQL Server – Concatenate Two Fields with a Space

While in the previous example, the requested result – merging two values from two different columns – has been achieved, the end result is still quite unreadable, as we have no space seperator between the first name and the last name. Therefore, it is advisable to also concatenate a space (‘ ‘) :

SELECT first_name + ' ' + last_name
FROM employees

The result :

(No column name)
-----------------------------
John Smith

 

Using a SQL Server Column Alias

To make the result more readable, use a SQL Server Column Aliases:

SELECT first_name + ' ' + last_name AS 'FullName'
FROM employees

The Result :

FullName
--------------
John Smith

 

SQL Server – Retrieving additional columns

In SQL Server, If after or before this concatenation you would like to display an additional separate column, simply use a comma (,):

SELECT city, first_name + ' ' + last_name AS 'FullName', salary
FROM employees

The result:

city    FullName    salary
------  ----------  -------------
London  John Smith  5800

 

SQL Server – Concatenate more than two values

In SQL Server it is possible to create more complicated concatenations as required:

SELECT 'Employee Name : ' + first_name + ' - '
+ last_name AS 'E_DETAILS',
FROM employees

The result :

EDetails
-----------------------------
Employee Name : John- Smith