SQL Server Tutorial SQL Server Advanced SQL Server Database SQL Server References

SQL Server - CONCAT() Function



The SQL Server (Transact-SQL) CONCAT() function is used to concatenate two or more strings together. This function may have two or more arguments.

Syntax

CONCAT(string1, string2, ... string_n)

Parameters

string1, string2, ... string_n Required. Specify the strings to concatenate together.

Return Value

Returns the concatenated string.

Example 1:

The example below shows the usage of CONCAT() function.

SELECT CONCAT('SQL ', 'Tutorial');
Result: 'SQL Tutorial'

SELECT CONCAT('SQL', ' ', 'Tutorial');
Result: 'SQL Tutorial'

SELECT CONCAT('Learning ', 'SQL ', 'is ', 'fun!.');
Result: 'Learning SQL is fun!.'

SELECT CONCAT('Sum is ', 25 + 25);
Result: 'Sum is 50'

SELECT CONCAT('Alpha ', 'Beta', ' Gamma');
Result: 'Alpha Beta Gamma'

SELECT CONCAT('Alpha ', 'Beta', ' Gamma', NULL);
Result: 'Alpha Beta Gamma'

Example 2:

Two additional single quotes within the surrounding single quotation can be used to concatenate single quotes. See the example below:

SELECT CONCAT('Let', '''', 's learn SQL Server');
Result: Let's learn SQL Server

Example 3:

Consider a database table called Employee with the following records:

EmpIDFirstNameLastName
1JohnSmith
2MarryKnight
3JoWilliams
4KimFischer
5RameshGupta
6HuangZhang

In the query below, the CONCAT() function is used to concatenate records of column FirstName and column LastName.

SELECT *, CONCAT(FirstName, ' ', LastName) AS FullName FROM Employee;

This will produce the result as shown below:

EmpIDFirstNameLastNameFullName
1JohnSmithJohn Smith
2MarryKnightMarry Knight
3JoWilliamsJo Williams
4KimFischerKim Fischer
5RameshGuptaRamesh Gupta
6HuangZhangHuang Zhang

❮ SQL Server Functions